js禁止瀏覽器的回退事件


直接來個終極方案:

查找了好多資料才找到的,這種方式,可以消除 后退的所有動作。包括 鍵盤、鼠標手勢等產生的后退動作。

<script language="javascript">
    //防止頁面后退
    history.pushState(null, null, document.URL);
    window.addEventListener('popstate', function () {
            history.pushState(null, null, document.URL);
    });
</script>

現在,我們項目中就使用了這種方式。在常用瀏覽器中,都可以禁用了后退。

 

具體實例:

$(function(){
    if (window.history && window.history.pushState) {
            history.pushState(null, null, document.URL);
            window.addEventListener('popstate', forbidBack);
    }
})
/**
 * 禁止回退按鈕
 */
function forbidBack(){
    appUtils.mobileConfirm("確定放棄重置密碼?",function(){//yes
        window.removeEventListener('popstate',forbidBack);
        muiwindow.muiwebview({"url":"login.html"});
    },function(){//no
        //防止頁面后退
        history.pushState(null, null, document.URL);
    });
}

 

PS:關於addEventListener與removeEventListener的注意事項可參考: http://www.cnblogs.com/JsonShare/p/6737499.html

      關於HTML5 history新特性pushState、replaceState : http://blog.csdn.net/tianyitianyi1/article/details/7426606

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM