Disable browser's back button

后端 未结 20 1406
猫巷女王i
猫巷女王i 2020-11-22 04:27

How to disable browser\'s BACK Button (across browsers)?

20条回答
  •  北海茫月
    2020-11-22 05:18

    The problem with Yossi Shasho's Code is that the page is scrolling to the top every 50 ms. So I have modified that code. Now its working fine on all modern browsers, IE8 and above

    var storedHash = window.location.hash;
    function changeHashOnLoad() {
        window.location.href += "#";
        setTimeout("changeHashAgain()", "50");
    }
    
    function changeHashAgain() {
        window.location.href += "1";
    }
    
    function restoreHash() {
        if (window.location.hash != storedHash) {
            window.location.hash = storedHash;
        }
    }
    
    if (window.addEventListener) {
        window.addEventListener("hashchange", function () {
            restoreHash();
        }, false);
    }
    else if (window.attachEvent) {
        window.attachEvent("onhashchange", function () {
            restoreHash();
        });
    }
    $(window).load(function () { changeHashOnLoad(); });
    

提交回复
热议问题