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

瀏覽器相容性

另見