js监听浏览器后退事件

参考链接:https://developer.mozilla.org/zh-CN/docs/Web/API/Window/onpopstate

代码如下:

$(document).ready(function(e) {

            var counter = 0;

            if (window.history && window.history.pushState) {

            window.onpopstate = function () {

                        window.history.pushState('forward', null, '#');

                        window.history.forward(1);

                        alert("不可回退");

                    };

            }

            window.history.pushState('forward', null, '#'); //在IE中必须得有这两行

            window.history.forward(1);

});

如果在浏览器执行了回退操作,则会触发页面弹出“不可回退”弹框,当然这里可以进行你所需要的不同的处理;



原文:https://www.jianshu.com/p/8916c3d8e07f

posted @ 2019-04-02 13:51  橱窗外的小孩  阅读(1296)  评论(0编辑  收藏  举报