js监听浏览器后退事件
$(document).ready(function(e){
varcounter=0;
if(window.history&&window.history.pushState){
$(window).on('popstate',function(){
window.history.pushState('forward',null,'#');
window.history.forward(1);
alert("不可回退");
});
}
window.history.pushState('forward',null,'#');//在IE中必须得有这两行
window.history.forward(1);
});
js后退怎么解决
虽然我们监听到了后退事件,但是页面还是会返回上一个页面,所以我们需要使用pushState增加一个本页的url,代表本页,大家都非常清楚是#
1
2
3
4
5
6
7
functionpushHistory(){
varstate={
title:"title",
url:"#"
};
window.history.pushState(state,"title","#");
}
当进入该页面,我们就给这个history压入一个本地的连接。当点击返回、后退及上一页的操作时,就进行监听,在监听代码中实现自己操作。
上一篇:登录token无效的解决方法
下一篇:纯css斜箭头的实现方法