onpopstate 特性为 WindowEventHandlers 混合 EventHandler 为处理 popstate events on the window.

A popstate event is dispatched to the window each time the active history entry changes between two history entries for the same document. If the activated history entry was created by a call to history.pushState() , or was affected by a call to history.replaceState() popstate event's state property contains a copy of the history entry's state object.

注意 : Calling history.pushState() or history.replaceState() won't trigger a popstate event. The popstate event is only triggered by performing a browser action, such as clicking on the back button (or calling history.back() in JavaScript), when navigating between two history entries for the same document.

句法

window.onpopstate = funcRef;
					
  • funcRef 是处理程序函数。

范例

For example, a page at http://example.com/example.html running the following code will generate alerts as indicated:

window.onpopstate = function(event) {
  alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};
history.pushState({page: 1}, "title 1", "?page=1");
history.pushState({page: 2}, "title 2", "?page=2");
history.replaceState({page: 3}, "title 3", "?page=3");
history.back(); // alerts "location: http://example.com/example.html?page=1, state: {"page":1}"
history.back(); // alerts "location: http://example.com/example.html, state: null
history.go(2);  // alerts "location: http://example.com/example.html?page=3, state: {"page":3}
					

Note that even though the original history entry (for http://example.com/example.html ) has no state object associated with it, a popstate event is still fired, when we activate that entry after the second call to history.back() .

规范

规范 状态 注释
HTML 实时标准
The definition of 'onpopstate' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
onpopstate Chrome 5 Edge 12 Firefox 4 IE 10 Opera 11.5 Safari 6 WebView Android 37 Chrome Android 18 Firefox Android 4 Opera Android 11.5 Safari iOS 5.1 Samsung Internet Android 1.0

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: