PopStateEvent: state 属性
PopStateEvent 接口的state 只读属性表示事件创建时存储的状态。
实际上,它是通过调用 history.pushState() 或 history.replaceState() 提供的值。
值
一个对象,或 null。
示例
下面的代码在使用 pushState() 方法向历史记录推送值时,记录了 state 的值。
js
// Log the state of
addEventListener("popstate", (event) => {
console.log("State received: ", event.state);
});
// Now push something on the stack
history.pushState({ name: "Example" }, "pushState example", "page1.html");
history.pushState(
{ name: "Another example" },
"pushState example",
"page1.html",
);
这将记录
State received: { name: "Example" }
State received: { name: "Another example" }
规范
| 规范 |
|---|
| HTML # dom-popstateevent-state-dev |
浏览器兼容性
加载中…
另见
PopStateEvent()构造函数History.state