PopStateEvent: state 属性

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

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

浏览器兼容性

另见