iT邦幫忙

2024 iThome 鐵人賽

DAY 11
1
佛心分享-IT 人自學之術

什麼都摸一點!拒絕當不沾鍋!系列 第 14

Day14 前端網頁開發(5) 簡單的登入+註冊頁面

  • 分享至 

  • xImage
  •  

No one comes to your website to be entertained. They have questions they think you can answer. Content answers questions.
― Jay Baer

分析需求:

  • 需要三個頁面:登入頁面、註冊頁面、以及登入完成後的頁面
  • 需要兩個按鈕:一個登入(登入驗證),一個註冊帳密
  • 需要兩個超連接:一個到註冊頁面、另一個回到登入頁面
  • 四個文字輸入框:(UserName和password) 登入兩個、註冊兩個

我們將三個頁面所對應到的三個JS檔案全部分檔撰寫,總共會有六個檔案,這樣比較方便管理:

login.html

<!-- login.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login Page</title>
</head>
<body>
    <h1>Login</h1>
    <!-- login-->
    <input type="text" id="username" placeholder="Username">
    <input type="password" id="password" placeholder="Password">
    <button id="loginBtn">Login</button>
    <p>Don't have an account? <a href="register.html">Register here</a></p>
    <!-- JavaScript file -->
    <script src="login.js"></script>
</body>
</html>

login.js

// login.js
document.getElementById('loginBtn').addEventListener('click', () =>{
    const enteredUsername = document.getElementById('username').value;
    const enteredPassword = document.getElementById('password').value;

    // Retrieve stored username and password from local storage
    const storedUsername = localStorage.getItem('username');
    const storedPassword = localStorage.getItem('password');

    // Validate credentials
    if (enteredUsername === storedUsername && enteredPassword === storedPassword) {
        alert('Login successful!');
        localStorage.setItem('loggedInUsername', enteredUsername);
        // Redirect to main.html
        window.location.href = 'main.html';
    } else {
        alert('Invalid credentials. Please try again.');
    }
});

register.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Registration Page</title>
</head>
<body>
    <h1>Register</h1>
    <!-- registration-->
    <input type="text" id="username" placeholder="Username">
    <input type="password" id="password" placeholder="Password">
    <button id="registerBtn">Register</button>
    <p>Already have an account? <a href="login.html">Login here</a></p>
    <!-- JavaScript file -->
    <script src="register.js"></script>
</body>
</html>

register.js

// register.js
document.getElementById('registerBtn').addEventListener('click', () => {
    const username = document.getElementById('username').value;
    const password = document.getElementById('password').value;
    if (username == "" || password == ""){
        alert("Username or Password can not be empty!");
        return;
    }

    // Store user data in local storage
    localStorage.setItem('username', username);
    localStorage.setItem('password', password);

    alert("Registration Success!");
    window.location.href = 'login.html';
});

main.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home</title>
</head>
<body>
    <h1 id="welcomeMsg"></h1>
    <script src="main.js"></script>
</body>
</html>

main.js

const loggedInUsername = localStorage.getItem('loggedInUsername');
if (loggedInUsername) {
    document.getElementById('welcomeMsg').textContent = `Your Grace, ${loggedInUsername}!`;
} else {
    console.log('User not logged in.');
}

可以看到我們使用了昨天結尾說到的localStroage這個東西,它可以把資料存放在瀏覽器中,這樣我們就算頁面刷新,帳密也不會消失!

右鍵→檢查→ >> → Application → Storage → Local storage 就可以看到我們存放的username和password!

[JavaScript] Cookie、LocalStorage、SessionStorage 差異 | by Peggy Chan | Medium

我們可以也可以補一個按鈕去清除LocalStorage的內容:

localStorage.removeItem('username');
localStorage.removeItem('password');

試試使用CSS美化吧!

下一篇進入小結!


上一篇
Day13 前端網頁開發(4) Todo List
下一篇
Day15 前端網頁開發(5) 小結
系列文
什麼都摸一點!拒絕當不沾鍋!30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言