javascripthtmlhtml4

Redirect to another html page using only html and javascript


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>

Solution

    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)?