js 监控浏览器回退

情况是这样的:手机浏览器访问项目时,当正常退出后,又没重新登录时,点击回退按键,浏览器会回退到之前的页面。
我之前在每一个页面都加了是否登录的验证。但是在手机端回退后还是会回退到上一个页面,我猜想页面应该是没有刷新的。因为我点击顶上的刷新按钮时,浏览器才回退到登录页。将下面的代码加入到login.jsp中。其他页面不用加。(傻傻的我一开始所有的页面都加上了,结果所有的页面的回退都不能正常使用了。。。)

//监听 浏览器的返回事件 
$(document).ready(function(e) { 
    var counter = 0;
    if (window.history && window.history.pushState) {
                     $(window).on('popstate', function () {
                                    window.history.pushState('forward', null, '#');
                                    window.history.forward(1);
                                    //填写逻辑
                                    window.location.href = "http://www.baidu.com";
                        });
      }

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

猜你喜欢

转载自blog.csdn.net/liguangix/article/details/81118215