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


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

            


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM