Search code examples
javascripthtmlcssdom-eventsvs-web-site-project

I am not able to load another html page which I have created as student.html after javascript validation


Please tell me which function I can use to open student.html when the username and password is matching with the value I described in scrip tag.

<form>
    <input
        name="username"
        type="text"
        id="txt_username"
        class="text1"
        ondrop="return false;" 
        onpaste="return false;"
        style="color:#0000C0;border-style:Solid;font-family:verdana;font-size:Small;"
    />
    <input
        name="password"
        type="password"
        id="txt_password"
        class="text1"
        style="color:#0000C0;background-color:White;;border-style:Solid;font-family:verdana;font-size:Small;"
    />  
    <input
        type="submit"
        value="Login"
        onclick="validate()"
    />
</form>

<script type="text/javascript">
function validate() {
    var text1 = document.getElementById("txt_username");
    var text2 = document.getElementById("txt_password");
    if (text1.value == "root" && text2.value == "root") {
        alert("ok")
        load("student.html");
    } else {
        alert("fail");
        load("error.html");
    }
}
</script>

Solution

  • Use:

     window.location.assign
    


    function validate() {
        var text1 = document.getElementById("txt_username");
        var text2 = document.getElementById("txt_password");
        if (text1.value == "root" && text2.value == "root") {
            window.location.assign("student.html");        
        } else {
            window.location.assign("error.html");                
        }
    }
    <html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    </head>
    <body>
    <form>
        <input
            name="username"
            type="text"
            id="txt_username"
            class="text1"
            ondrop="return false;" 
            onpaste="return false;"
            style="color:#0000C0;border-style:Solid;font-family:verdana;font-size:Small;"
        />
        <input
            name="password"
            type="password"
            id="txt_password"
            class="text1"
            style="color:#0000C0;background-color:White;;border-style:Solid;font-family:verdana;font-size:Small;"
        />  
        <input
            type="submit"
            value="Login"
            onclick="validate()"
        />
    </form>
    </body>
    </html>