-4

How to go to another page using only html and css. I have tried with

window.location.href="admin.html";

This statement not working.

<input type="text" value="Username" name="username" /><br>
<input type="text" value="Password" name="password" /><br>
<input type="button" value="Password" name="password" value="Log In" onClick="login()" /><br>

<script>
    function login() {
        var id = document.getElementById("username").value;
        var pass = document.getElementById("password").value;
        if (id == "admin" && pass = "admin")
            //Redirect to admin page
        else if (id == "employee" && pass = "employee")
        //Redirect to employee page
    }
</script>
Krupesh Kotecha
  • 2,266
  • 2
  • 17
  • 39

2 Answers2

2

Your problem has nothing to do with the code you use to navigate. You never get that far.

Look at the developer tools in your browser. Examine the Console. You should see an error message like:

Uncaught TypeError: Cannot set property 'value' of null

document.getElementById("username") fails to find your element because you have no element with that id. The name and the id are different attributes and are not freely interchangable.

You have the same problem for the password field.

Quentin
  • 800,325
  • 104
  • 1,079
  • 1,205
1
  1. You have given the fields a name, not an id so your code will throw an error before reaching the redirect.

  2. Is admin.html in the same scope/folder as this file? If not, you will need to specify the relative path to admin.html, eg: ../otherFolder/admin.html

  3. Have you tried using window.location.replace(admin.html)?

James Whiteley
  • 3,173
  • 1
  • 13
  • 35