js禁止浏览器的回退事件
直接来个终极方案:
查找了好多资料才找到的,这种方式,可以消除后退的所有动作。包括键盘、鼠标手势等产生的后退动作。
//防止页面后退 history.pushState(null,null,document.URL); window.addEventListener('popstate',function(){ history.pushState(null,null,document.URL); });
现在,我们项目中就使用了这种方式。在常用浏览器中,都可以禁用了后退。
具体实例:
$(function(){
if(window.history&&window.history.pushState){
history.pushState(null,null,document.URL);
window.addEventListener('popstate',forbidBack);
}
})
/**
*禁止回退按钮
*/
functionforbidBack(){
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()解析
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下