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

瀏覽器相容性

另見