直接來個終極方案:
查找了好多資料才找到的,這種方式,可以消除 后退的所有動作。包括 鍵盤、鼠標手勢等產生的后退動作。
<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的注意事項可參考:addEventListener()與removeEventListener()解析
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答