如何禁用浏览器前进按钮,我有脚本禁用后退...?

时间:2016-02-19 11:41:58

标签: javascript html asp.net-mvc

以下代码用于禁用浏览器中的后退按钮。

onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload=""

<script type = "text/javascript" >
     window.history.forward();
     function noBack() {
         window.history.forward();
         window.onload = window.history.forward(0);
         window.onbeforeunload = function () { return "You work will be lost."; };
     }
</script>

我无法为前进按钮执行此操作。

1 个答案:

答案 0 :(得分:0)

在javascript中试用此代码

    <script type = 'text/javascript' >
        function changeHashOnLoad() {
           window.location.href += '#';
           setTimeout('changeHashAgain()', '50'); 
        }
        function changeHashAgain() {
            window.location.href += '1';
        }
        var storedHash = window.location.hash;
        window.setInterval(function () {
        if (window.location.hash != storedHash) {
            window.location.hash = storedHash;
         }
         }, 50);
         window.onload=changeHashOnLoad;
   </script>

替代方法

<SCRIPT type='text/javascript'>
    window.history.forward();
    function noBack() {window.history.forward(); }
    window.onload=noBack;
    window.onpageshow=function(evt){if(evt.persisted)noBack()}
    window.onunload=function(){void(0)}
</SCRIPT>

希望这有帮助