Window: storage event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2017.

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.

  • For localStorage, the event is fired in all other browsing contexts that are in the same origin as the initiating document. This includes other tabs with the same origin.
  • For sessionStorage, the event is fired in all other browsing contexts that are in the same origin and the same top-level browsing context as the initiating document. This only includes embedded iframes, if any, in the same tab, and not other tabs.

This event is not cancelable and does not bubble.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

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

onstorage = (event) => { }

Event type

A StorageEvent. Inherits from Event.

Event StorageEvent

Event properties

key Read only

Returns a string with the key for the storage item that was changed. The key attribute is null when the change is caused by the storage clear() method.

newValue Read only

Returns a string with the new value of the storage item that was changed. This value is null when the change has been invoked by storage clear() method, or the storage item has been removed from the storage.

oldValue Read only

Returns a string with the original value of the storage item that was changed. This value is null when the storage item has been newly added and therefore doesn't have any previous value.

storageArea Read only

Returns a Storage object that represents the storage object that was affected.

url Read only

Returns string with the URL of the document whose storage changed.

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:

js
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:

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

Specifications

Specification
HTML
# event-storage
HTML
# handler-window-onstorage

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
storage event
Chrome – Full support
Chrome 1 (Release date: 2008-12-11)
footnote Full support
Edge – Full support
Edge 15 (Release date: 2017-04-05)
footnote Full support
Firefox – Full support
Firefox 45 (Release date: 2016-03-08)
footnote Full support
Opera – Full support
Opera 15 (Release date: 2013-07-02)
footnote Full support
Safari – Full support
Safari 4 (Release date: 2009-06-08)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
footnote Full support
Firefox for Android – Full support
Firefox for Android 45 (Release date: 2016-03-08)
footnote Full support
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
footnote Full support
Safari on iOS – Full support
Safari on iOS 4 (Release date: 2010-06-21)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
footnote Full support
WebView on iOS – Full support
WebView on iOS 4 (Release date: 2010-06-21)
footnote Full support
Deno – No support
Deno
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support

See also