Search code examples
javascripthtmlauthenticationlogin-script

My javascript isn't working on the login page only refreshes?


At first it worked but then it stopped working can't find the error please help! When you write in the correct username and password it only puts it in the url and doesn't work. The demo version has css -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------> DEMO <----------------------------------------------------------------

/*This Script allows people to enter by using a form that asks for a
UserID and Password*/
function pasuser(form) {
if (form.identifier.value=="GG") { 
if (form.pass.value=="123") { 
  window.location('https://www.google.com/');
} else {
alert("Invalid Password");
}

} else {  alert("Invalid UserID");
}
}
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="/style.css">
  <script src="/server.js"></script>
  <title></title>
</head>
<body>
  <div class="box">
    <h2>Login</h2>
    <form>
      <div class="inputBox">
        <input type="text" name="identifier" required="">
        <label>Username</label>
      </div>
      <div class="inputBox">
        <input type="password" name="pass" required="">
        <label>Password</label>
      </div>
      <input type="button" value="Submit" onclick="pasuser(form)">
    </form>
  </div>
</body>
</html>


Solution

  • window.location is not a function. try window.location.assign(.....

    /*This Script allows people to enter by using a form that asks for a
    UserID and Password*/
    function pasuser(form) {
    if (form.identifier.value=="GG") { 
    console.log(form.identifier.value)
    if (form.pass.value=="123") { 
      window.location.assign('https://www.google.com/');
    } else {
    alert("Invalid Password");
    }
    
    } else {  alert("Invalid UserID");
    }
    }
    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <link rel="stylesheet" href="/style.css">
      <script src="/server.js"></script>
      <title></title>
    </head>
    <body>
      <div class="box">
        <h2>Login</h2>
        <form>
          <div class="inputBox">
            <input type="text" name="identifier" required="">
            <label>Username</label>
          </div>
          <div class="inputBox">
            <input type="password" name="pass" required="">
            <label>Password</label>
          </div>
          <input type="button" value="Submit" onclick="pasuser(form)">
        </form>
      </div>
    </body>
    </html>