2016-07-30 51 views
0

我正在創建一個需要密碼才能訪問的站點,但一旦密碼提示啓動後,返回就太困難(不方便用戶了)。密碼的目的是防止用戶看到頁面和頁面源。
這裏是我迄今:如果按下取消,關閉標籤/重定向?

 do{ 
      $('html').hide(); //hiding html with jQuery 
      var pass = "password"; //setting password 
      var selection = prompt("Enter password", ""); //prompting for password 
      if(selection == "cancel"){ 
       window.location.href = "http://www.example.com/"; 
      }; //trying (unsuccesfully) to make the page redirect if user answers "cancel" 
     } while(selection!=pass); 

回答

0

https://developer.mozilla.org/en-US/docs/Web/API/Window/prompt

If the user clicks the Cancel button, this function returns null.

所以,

do{ 
    $('html').hide(); 
    var pass = "password"; 
    var selection = prompt("Enter password", ""); 
    if(selection === null) { 
    window.location.href = "http://www.example.com/"; 
    break; 
    }; 
} while(selection !== pass); 

您需要break語句,因爲如果有瀏覽器不會重定向仍然運行代碼,就像這裏的while循環一樣。

+0

我需要它重定向時,取消按鈕被按下,這是不是在我的測試中發生 –

+0

我更新了答案:) – hansottowirtz

+0

非常感謝:D –