开发者

JavaScript: How to redirect a page after validation

开发者 https://www.devze.com 2023-03-04 04:43 出处:网络
I want to redirect a page after validation. I have the ff. EXAMPLE: form.html: <form method=\"post\" action=\"\" enctype=\"multipart/form-data\" onsubmit=\"return checkform(this);\">

I want to redirect a page after validation. I have the ff. EXAMPLE:

form.html:

<form method="post" action="" enctype="multipart/form-data" onsubmit="return checkform(this);">
<p><span style="width:180px">Username: </span><input type="text" name="username" id='un'><开发者_JS百科/p>
<p><span style="width:180px">Password: </span><input type="password" name="password" id='pw'></p>
<input type="submit" value="SUBMIT" />
</form>

script:

<script type='text/javascript'>
function checkform(){
    if(document.getElementById("un").value == 'jayem30' && document.getElementById("pw").value == 'jayem' ){
        alert("Login Successful");
        window.location = "http://www.google.com/"
    }else{
        alert("Access denied. Valid username and password is required.");
    }
}
</script>

I tried this but it does not redirect to google.com. Many thanks for any help! :)


Try putting redirect in a timeout. Works like a charm

setTimeout(function() {window.location = "http://www.google.com/" });

By the way, instead of

onsubmit="return checkform(this);"

use

onsubmit="return(checkform())"

because IE doesn't like when you ommit ( and ).


try document.location.href instead of window.location


Try out this method to redirect page after validation.

syntax:

window.location.assign("url")

example:

<script type='text/javascript'>

function checkform(){

    if(document.getElementById("un").value == 'jayem30' && document.getElementById("pw").value == 'jayem' ){
        alert("Login Successful");
        window.location.assign("http://www.google.com/")
    }else{
        alert("Access denied. Valid username and password is required.");
    }
}
</script>

This is a working method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号