当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js禁止浏览器的回退事件

js禁止浏览器的回退事件

2018年05月20日  | 移动技术网IT编程  | 我要评论

直接来个终极方案:

查找了好多资料才找到的,这种方式,可以消除 后退的所有动作。包括 键盘、鼠标手势等产生的后退动作。

<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()解析

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网