一、當活動歷史記錄條目更改時,將觸發popstate事件。
若是被激活的歷史記錄條目是經過對history.pushState()的調用建立的,
或者受到對history.replaceState()的調用的影響,popstate事件的state屬性包含歷史條目的狀態對象的副本。
二、須要注意的是調用history.pushState()或history.replaceState()用來在瀏覽歷史中添加或修改記錄。不會觸發popstate事件;
只有在作出瀏覽器動做時,纔會觸發該事件,如用戶點擊瀏覽器的回退按鈕(或者在Javascript代碼中調用history.back())
if (window.history && window.history.pushState) { window.onpopstate = function(event) { console.log("location: " + document.location + ", state: " + JSON.stringify(event.state)); //window.history.go(1) //window.history.back() }; //window.addEventListener("popstate", function(e) { // window.location = 'http://www.baidu.com'; //}, false); !function() { var state = { title: "title", url: "#" }; window.history.pushState(state, "title", "#"); }(); }