最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

html - JavaScript: How to redirect a page after validation - Stack Overflow

matteradmin3PV0评论

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'></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 = "/"
    }else{
        alert("Access denied. Valid username and password is required.");
    }
}
</script>

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

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'></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./"
    }else{
        alert("Access denied. Valid username and password is required.");
    }
}
</script>

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

Share asked May 11, 2011 at 3:35 KrisKris 3,77916 gold badges52 silver badges67 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

Try putting redirect in a timeout. Works like a charm

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

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./")
    }else{
        alert("Access denied. Valid username and password is required.");
    }
}
</script>

This is a working method.

Post a comment

comment list (0)

  1. No comments so far