2015-07-21 77 views
-2

我想讓此代碼重定向到另一個頁面,當人員輸入hello時。我該怎麼辦? 如何在javascript中爲我的html網站設置密碼

<p>Click the button to demonstrate the prompt box.</p> 

<button onclick="myFunction()">Try it</button> 

<p id="demo"></p> 

<script> 
function myFunction() { 
    var person = prompt("Please enter your password"); 

    if (person == hello) { 
     window.open("http://www.w3schools.com"); 
    } 
    else { 
     confirm("Invalid Password")} 
} 
</script> 
</body> 
</html> 
+1

問題與標題完全無關。 – Scott

+1

使用javascript(或任何客戶端腳本)來控制密碼訪問通常是一個非常糟糕的主意。您可能不太安全的唯一方法是直接在頁面上打印密碼供所有用戶查看。 – nomistic

+0

這只是一個實驗密碼項目 –

回答

0

像其他人所說的,這實在是不安全的。但是對於這個項目的緣故,試試這個:

<p>Click the button to demonstrate the prompt box.</p> 

<button onclick="myFunction()">Try it</button> 

<script> 
function myFunction() { 
    var person = prompt("Please enter your password"); 

    if (person == 'hello') { 
     document.location.href = "http://www.w3schools.com"; 
    } 
    else { 
     confirm("Invalid Password") 
    } 
} 
</script> 
  1. 確保周圍添加引號你好,因爲它要比較的人變量的字符串。

  2. 而不是使用window.open(),只需將document.location.href屬性更改爲新的URL。這會將新的URL加載到當前窗口中。 Window.open()會打開一個名稱所暗示的新瀏覽器窗口,但由於彈出窗口阻止程序,該窗口並不總是有效。

-1

只是試試這個

<button onClick="window.location='page_name.php';" value="click here" /> 
+0

這並不能真正回答這個問題嗎? –

相關問題