Search code examples
javascriptpasswordslogin-script

JavaScript password and page variation


I am using this code for easy password protection of web pages but I want to edit the code so I can have two different passwords opening two different web pages. So when I enter "Password No.1" it will open "Page No.1" but if I type "Password No.2" it will open "Page No.2".

<script>

function TheLogin() {

var password = 'password1';

if (this.document.login.pass.value == password) {
  top.location.href="pageno1.php";
}
else {
  window.alert("Incorrect password, please try again.");
  }
}

</script>

Solution

  • <script>
    
    function TheLogin() {
    
    if (this.document.login.pass.value == 'password1') {
      top.location.href="pageno1.php";
    }
    else if (this.document.login.pass.value == 'password2') {
      top.location.href="pageno2.php";
    }
    else {
      window.alert("Incorrect password, please try again.");
      }
    }
    
    </script>