2014-12-03 59 views
0

這是我的`的index.html的片段的JavaScript window.location的不是重定向到一個不同的頁面

<div class="maincontainer"> 
    <h2 id="heading" class="heading"></h2> 
    <input type="button" id="resetPassword" class="resetPassword" onclick="resetPassword()" value= "Reset Password" /> 
    <form method="post" onsubmit="return submitform()"> 
     <div> 
      <input type="password" id="password" name="password" class="patternlock" /> 
      <input type="submit" value="login"/> 
     </div> 
    </form> 
</div> 

這是我的script.js,我包括在我的index.html的片段

function submitform(){ 
var passwordvalue = document.getElementById("password").value; 
var digits = getlength(passwordvalue); 
if(digits<5) { 
    raiseerror("lengthTooSmall"); 
} 

checkduplicatedigits(passwordvalue); 

if (errorraised == false && passwordset == false) { 
    localStorage.setItem("passwordvalue", passwordvalue); 
    successmessage("patternStored"); 
} 
else if (errorraised == false && passwordset == true) { 
    if (localStorage.getItem("passwordvalue") == passwordvalue) { 
     successmessage("screenUnlocked"); 
    } 
    else { 
     raiseerror("IncorrectPattern"); 
    } 
} 
return true; 
}; 

function successmessage(successcode) { 
    if(successcode == "screenUnlocked") { 
     alert("You have unlocked the screen!"); 
     window.location = "./welcome.html"; //bug to be fixed 
    } 
    if (successcode == "patternStored") { 
     alert("Your pattern is stored. Thanks."); 
     passwordset = true; 
    } 
    if (successcode == "resetSuccess") { 
     alert("Pattern Reset Success!"); 
    } 
    location.reload(); 
}; 

此時,每當,successcode == 「screenUnlocked」 被觸發時,我能夠得到警報消息

alert("You have unlocked the screen!"); 

但下一行

window.location = "./welcome.html"; 

不起作用。

爲什麼不在這種情況下工作? 是否有任何具體要求爲window.location工作?如果是這樣,請說明。

查看源代碼這個代碼在谷歌瀏覽器39.0.2171.71。我檢查了我的控制檯錯誤但是沒有任何。只有幾個警告,但它們與此無關。

還是有任何其他功能使用純Javascript進行重定向?如果是這樣,請說明。

如果需要更多信息,請發表評論。

+0

'welcome.html'文件位於何處?在'index.html'中是一樣的嗎? – azhpo 2014-12-03 13:28:50

+0

恩,我希望你知道這段代碼根本不安全。 – epascarello 2014-12-03 13:31:14

+0

@azhpo是的,當然。 – 2014-12-03 15:44:04

回答

1

首先,這是不安全的,所以我希望這不是真正的保護。

它失敗的原因是你有一個與window.location和表單提交的競爭條件。表單提交是因爲您沒有取消表單提交。

您需要在驗證函數結束時返回false,而不是true。

+0

感謝您的信息。除了當調用'successmessage(「screenUnlocked」);'時,我必須在所有其他情況下提交表單。因此submitform()返回false不起作用。 – 2014-12-03 16:10:49

+0

'return true'你需要它提交。當你需要失敗時返回false。 – epascarello 2014-12-03 16:12:33

+0

是的,沒錯。但我真的很困惑,看到返回錯誤不會阻止提交表單。從而不讓頁面重定向。 – 2014-12-03 16:28:36

0

回答取代

successmessage("screenUnlocked"); 

通過

successmessage("screenUnlocked"); 
return false; 
+0

但是我不得不承認,我沒有得到你的意圖,你想提交表單,還是你想提交被中止,而是重定向到另一個頁面?你喜歡在其他情況下留在同一頁面嗎? – 2014-12-03 13:36:15

+0

是的,除了調用'screenmessage(「screenUnlocked」)'時,我想在每種情況下都提交表單。在所有其他情況下,應提交表格。 – 2014-12-03 16:07:57

+0

'successmessage(「screenUnlocked」); 返回false;'不工作。我真的很困惑,看到這一點。 – 2014-12-03 16:09:26

相關問題