Window: storage event
The storage event of the Window interface fires when a storage area (localStorage) has been modified in the context of another document.
Note: This won't work on the same page that is making the changes — it is really a way for other pages on the domain using the storage to sync any changes that are made. Pages on other domains can't access the same storage objects.
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 properties
keyRead only-
Returns a string that represents the key changed. The
keyattribute isnullwhen the change is caused by the storageclear()method. newValueRead only-
Returns a string with the new value of the
key. This value isnullwhen the change has been invoked by storageclear()method, or thekeyhas been removed from the storage. oldValueRead only-
Returns a string with the original value of the
key. This value isnullwhen thekeyhas been newly added and therefore doesn't have any previous value. storageAreaRead only-
Returns a
Storageobject that represents the storage that was affected. urlRead only-
Returns string with the URL of the document whose
keychanged.
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 Standard # event-storage |
| HTML Standard # handler-window-onstorage |
Browser compatibility
BCD tables only load in the browser