源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

js禁止浏览器的回退事件

  • 时间:2020-01-21 23:40 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要: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的注意事项可参考:[url=http://www.1sucai.cn/article/111662.htm]addEventListener()与removeEventListener()解析[/url] 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部