2016-11-10 40 views
0

首先,我只想說我對html不太好,我所知道的關於html的所有內容都來自我自己在線學習的東西。如果條件爲真,則將用戶重定向到另一個html頁面

所以..我有一個項目,其中一部分是製作一個網站。我已經做了網站,但我有一個問題..

我的問題是我不想讓用戶訪問該設置,我希望管理員只能訪問該設置,我通過將密碼設置頁面..所以如果用戶點擊設置圖標,它會要求輸入密碼,如果其正確,那麼用戶可以去設置頁面

我有3頁(index.html,這是我的主頁) ,(我的設置代碼所在的manager.html)以及(index4代碼所在的位置) in index.html有一個代碼,讓用戶轉到index4.html頁面 並在index4.html頁面中輸入代碼

<!DOCTYPE html> 

<html> 

<body> 

    <script> 
     var pass; 
     pass = prompt("please enter the password to access"); 
     if (pass == "11111"){ 
      <a href="manager.html">This is the link.</a>   
     } else { 
      document.write("The password you've is enter is incorrect"); 
     } 
</script> 

<body> 

</html> 

從我在網上學到的信息,href =「link」代碼可以重定向到另一個頁面......但似乎我不能在'if語句'中使用它。 當我做了檢查元素,我得到了這個錯誤「意外的令牌」<'「

有人可以告訴我如何糾正我的錯誤。

+0

你不能在一個JavaScript塊中使用HTML ....'location'是你需要檢查什麼 –

回答

1

嘗試設置document.location.href - 更多對here

var pass; 
 
pass = prompt("please enter the password to access"); 
 
if (pass == "11111") { 
 
    document.location.href = "manager.html"; 
 
} else { 
 
    document.write("The password you've is enter is incorrect"); 
 
}

2

另一種方式是作爲波紋管使用。勾選此link

例如:

// similar behavior as an HTTP redirect 
window.location.replace("http://stackoverflow.com"); 

// similar behavior as clicking on a link 
window.location.href = "http://stackoverflow.com"; 
相關問題