0

I have a login.html file and a main.html file, when I enter the username and password and click login in the login.html, I need it to go to the main.html, using javascript

I have this in the login.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width", initial-scale=1.0>
    <title>Login</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <header class="container">
        <form class="form" id="login">

            <h1 class="form__title">Login</h1>
            
            <div class="form__input-group">
                <input type="text" class="form__input" id="username" autofocus placeholder="Username">
            </div>
            <div class="form__input-group">
                <input type="password" class="form__input" id="password" placeholder="Password">
            </div>

            <button class="form__button" type="submit" onclick="LogIn()">Log in</button>

        </form>
    </header>
    <script src="index.js"></script>
</body>
</html>

this is the javascript

var loginAcc = [
        {
            username: 'username123',
            password: 'password123',
        },
        {
            username: 'username456',
            password: 'password456'
        }
    ]

function LogIn() {
    var username = document.getElementById("username").value;
    var password = document.getElementById("password").value;
    console.log(`Username: ${username}`);
    console.log(`Password: ${password}`);

    for(i = 0;i < loginAcc.length; i++) {
        if(username == loginAcc[i].username && password == loginAcc[i].password) {
            console.log(`${username} is logged in`)
        }
    }
}

And how do I use javascript to open the main.html once the user has logged in

  • Do you work with nodejs? I would recommend to take a look at it. – Aalexander Feb 05 '21 at 07:28
  • 1
    Does this answer your question? [How to go from one page to another page using javascript?](https://stackoverflow.com/questions/4426184/how-to-go-from-one-page-to-another-page-using-javascript) – Udith Gunaratna Feb 05 '21 at 07:29

1 Answers1

0

You will need the JS code below for redirectionon and the "homepage.html" is just a sample. You can put in the name of the file that you want to redirect to. Please note that both files must be under the same file structure.

window.location.href = "homepage.html";
Tan Yi Jing
  • 295
  • 1
  • 8