2016-07-30 14 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 –