使用js代码禁用chrome浏览器的后退按钮


当时做一个单页应用, 由于用户老是喜欢按浏览器的后退按钮来返回上一步操作,但是单页应用返回上一步操作一般是不跳转页面的,所有我就想禁用浏览器的返回按钮功能来限制用户的操作习惯.

1.这里使用了jquery,先引入jquery库.

<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>

2.功能函数,将这个函数放到页面的<script>脚本中,接着在函数定义后调用这个函数即可.

<script>
  function disableBack() {
    if (window.history && window.history.pushState) {
      $(window).on("popstate", function () {
        window.history.pushState("forward", null, "");
        window.history.forward(1);
      });
    }
    window.history.pushState("forward", null, "");
    window.history.forward(1);
  }
  disableBack();
</script>


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM