History: state 属性

Baseline 已广泛支持

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

stateHistory 接口的一个只读属性,它返回一个值,表示历史堆栈顶部的状态。这是一种查看状态的方式,无需等待 popstate 事件。

历史堆栈顶部的状态。在使用 pushState()replaceState() 方法之前,该值始终为 null

示例

下面的代码在调用 pushState() 方法将值推入历史记录之前,记录了 history.state 的值。下一行代码再次将值记录到控制台,显示 history.state 现在具有了一个值。

js
// Should be null because we haven't modified the history stack yet
console.log("History.state before pushState: ", history.state);

// Now push something on the stack
history.pushState({ name: "Example" }, "pushState example", "page3.html");

// Now state has a value.
console.log("History.state after pushState: ", history.state);

规范

规范
HTML
# dom-history-state-dev

浏览器兼容性

另见