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

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 最要命的是 监听到事件  执行自定义函数前  页面突然跳走了  于是决定放弃

            

posted @ 2017-04-13 18:15  海边草  阅读(7419)  评论(0编辑  收藏  举报