2014-12-05 29 views
0

我需要防止用戶嚴格切換回到上一頁。我告訴你例子編碼如下:阻止瀏覽器和警告,嚴格的方式需要

<html> 
<head> 
<script type="text/javascript"> 

/* prevent back */ 
window.history.forward(); 
    function prevent() 
    { 
window.history.forward(); 
    } 


/* warning on unload */ 
window.onbeforeunload = function (e) { var message = 'Are you sure ?'; if (typeof e == 'undefined') { e = window.event; } if (e) { e.returnValue = message; } return message; } 


</script> 
</head> 
    <body onload="prevent();" onpageshow="if (event.persisted) prevent();" onunload=""> 
     <!--content here--> 
    <a href="in.html">in</a> 
    </body> 
</html> 

上述頁面有鏈接到另一個網頁,如果我點擊後面的是第二頁上,它回過頭來此第一頁,並顯示警告,然後移動到第二頁;

第二頁:

<a href="hello.html">hello</a> 

我添加某種原因警告腳本。

現在,我需要的是。當防止倒車時,警告腳本不應該起作用。

1.當用戶卸載頁面,應該顯示警告腳本 2.當防止反函數卸載頁面,不應該顯示警告腳本

有沒有什麼辦法可以禁止從所有頁面回去方式,任何嚴格的方式?上面的編碼並不是很嚴格。它將繼續前進。

謝謝。

+0

沒有辦法100%限制的用戶操作一個瀏覽器。如果用戶禁用JavaScript,該怎麼辦?您需要在後端(服務器端)採取措施進行此類操作。 – bansi 2014-12-05 05:54:11

+0

好吧,它好嗎,當另一個功能觸發時,我們如何防止功能 – 2014-12-05 05:55:31

+0

我有一個體面的答案烹飪。在一秒鐘 – Todd 2014-12-05 06:00:44

回答

0

是的,boi。

但嚴重的是,這個孩子不回來

頁1

<html> 
    <head> 
     <title>if you go, don't come back</title> 
    </head> 
    <body> 
      <!--content here--> 
     <a href="pageofnoreturn.html#noBack">in</a> 
    </body> 
</html> 

pageofnoreturn.html

<html> 
    <head> 
     <title>Stay put, or else</title> 
    </head> 
    <body> 
     <h1>welcome to purgatory</h1> 
     <script> 
      if (location.hash == '#noBack') { 
       history.pushState(null, '', '#sit'); 
       //whatever hashes that suit your project 
       window.onhashchange = function() { 
        if (location.hash == '#noBack') { 
         history.pushState(null, '', '#sit'); 
        } 
       } 
      } 
     </script> 
    </body> 
</html> 
+0

真棒託德,它的工作完美! – 2014-12-05 06:52:53

+0

我不需要那麼做,所以這很有趣。樂意效勞! – Todd 2014-12-05 07:39:43

+0

謝謝!託德。提問更有趣! – 2014-12-05 08:55:48