2011-05-11 30 views
2

我想在驗證後重定向頁面。我有ff。實例:JavaScript:驗證後如何重定向頁面

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

我試過,但它不重定向到google.com。非常感謝您的幫助! :)

回答

4

嘗試將超時重定向。工程就像一個魅力

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

順便說一句,而不是

onsubmit="return checkform(this);" 

使用

onsubmit="return(checkform())" 

,因爲當你ommit(和)IE不喜歡。

0

嘗試document.location.href而不是window.location

+0

應該是window.location.href – 2011-05-11 04:29:58

+0

document.location.href也可以工作 – Amareswar 2011-05-11 05:03:43

+1

document.location不適用於所有瀏覽器。 window.location確實。 – 2011-05-11 18:10:10