瀏覽器返回按鈕點擊事件觸發


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