Window: storage event


目录

The storage event of the Window interface fires when another document that shares the same storage area (either localStorage or sessionStorage) as the current window updates that storage area. The event is not fired on the window that made the change.

This event is not cancelable and does not bubble.

Syntax

Use the event name in addEventListener():

addEventListener("storage", (event) => { })

Alternatively, assign to the event handler property of the interface:

onstorage = (event) => { }

Event type

A StorageEvent. Inherits from Event.

Event handler aliases

In addition to the Window interface, the event handler property onstorage is also available on the following targets:

Examples

Log the sampleList item to the console when the storage event fires:

window.addEventListener("storage", () => {
  // When local storage changes, dump the list to
  // the console.
  console.log(JSON.parse(window.localStorage.getItem("sampleList")));
});

The same action can be achieved using the onstorage event handler property (but note that this replaces other event listeners added using onstorage, if any):

window.onstorage = () => {
  // When local storage changes, dump the list to
  // the console.
  console.log(JSON.parse(window.localStorage.getItem("sampleList")));
};

Specifications

Browser compatibility

See also


版本号 #3
由 壳壳虫 创建于 2026-10-31 03:27:53 UTC
由 cocobook-sync 更新于 2026-11-04 17:35:23 UTC