PopStateEvent:state 属性

statePopStateEvent 接口的只读属性,表示创建事件时存储的状态。

实际上,它是通过调用 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

浏览器兼容性

BCD 表格仅在启用 JavaScript 的浏览器中加载。

另请参阅