浏览器返回按钮点击事件触发

var triggerfn = function(event) {
  //alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
  $('#gohist').trigger('touchend');
};
window.addEventListener('popstate', triggerfn, false);
 
history.replaceState(null, null, window.location.pathname);
history.pushState(null, null, window.location.pathname);
 

popstate 点击浏览器 前进 后退 执行history.go()   history.back()  history.forword()  都会执行 

所以页面如果有 history.back()    执行之前要进行移除监听事件

window.removeEventListener('popstate', triggerfn, false);
history.back();

几轮测试发现  popstate 执行前提 是有 pushState   但是会记录到历史记录中  所以进入页面 使用replaceState  目前没想到好办法

 

对于 beforeunload 事件  页面销毁之前执行  

测试发现

1 里面的alert事件不执行  不利于开发

2 只要页面有跳转都会执行  要处理不必要的a点击等等

3 最要命的是 监听到事件  执行自定义函数前  页面突然跳走了  于是决定放弃

            

转载于:https://www.cnblogs.com/haibiancao/p/6705341.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值