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 最要命的是 監聽到事件 執行自定義函數前 頁面突然跳走了 於是決定放棄