js禁止瀏覽器的回退事件
更新時間:2017年04月20日 11:19:38 作者:_Json
這篇文章主要為大家詳細介紹了js禁止瀏覽器的回退事件瀏覽器回退按鈕事件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
直接來個終極方案:
查找了好多資料才找到的,這種方式,可以消除 后退的所有動作。包括 鍵盤、鼠標手勢等產(chǎn)生的后退動作。
<script language="javascript"> //防止頁面后退 history.pushState(null, null, document.URL); window.addEventListener('popstate', function () { history.pushState(null, null, document.URL); }); </script>
現(xiàn)在,我們項目中就使用了這種方式。在常用瀏覽器中瀏覽器回退按鈕事件,都可以禁用了后退。
具體實例:
$(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:關于與的注意事項可參考:()與()解析
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。