我有一個非常基本的代碼,我有兩個HTML頁面,index.html
是基本認證頁面,crap.html
是我做的頁面,好吧,一些廢話。
現在,我在index.html
上有一個表格,它附有onsubmit()
處理程序,以便我可以檢查用戶名和密碼。如果正確,它會顯示一條消息並繼續將用戶重定向到crap.html
。
現在,問題是此重定向似乎對錯誤的憑據正常工作,但是如果給出正確的憑據,它將自動重新加載index.html
頁面,即使在auth.js
中沒有重定向代碼時也是如此。JS重定向不能按預期工作
下面是代碼:
auth.js
function passCheck(){
var user = document.getElementById("username").value;
var pass = document.getElementById("password").value;
var error = document.getElementById("error");
var set_user = "admin";
var set_pass = "admin";
if(user==set_user && pass==set_pass){
error.style.color="green";
error.innerHTML = "AUTHENTICATED CORRECTLY. REDIRECTING NOW.";
setTimeout(function(){
window.location.href="../crap.html";
}, 3000);
}else{
error.style.color="red";
error.innerHTML = "WRONG CREDENTIALS. REDIRECTING NOW.";
setTimeout(function(){
window.location.href="../index.html";
}, 1000);
}
}
index.html
<html>
<head>
<title>Authentication</title>
<script src="js/jquery.js"></script>
<script src="js/auth.js"></script>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<form onsubmit="passCheck()">
<table border=0>
<tr>
<td>Username</td>
<td><input type="text" id="username" placeholder="Username" required autofocus></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" id="password" required></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="AUTHENTICATE">
</td>
</tr>
</table>
</form>
<center>
<div id="error"></div>
</center>
</body>
</html>
你有沒有嘗試把完整的URL而不是.../crap.html?可能潛在地縮小問題的範圍。 – ltalhouarne 2014-08-31 20:38:15
試過了。不工作。 – 2014-08-31 20:38:53
@lolkidoki即使我遺漏了重定向部分,它仍然會自動重新加載頁面。 – 2014-08-31 20:42:35