The storage
event of the Window
interface fires when a storage area (localStorage
or sessionStorage
) 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.
addEventListener("storage", (event) => {});
onstorage = (event) => {};
Event type
Event properties
key
Read only
-
Returns a string that represents the key 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 key
. This value is null
when the change has been invoked by storage clear()
method, or the key
has been removed from the storage.
oldValue
Read only
-
Returns a string with the original value of the key
. This value is null
when the key
has been newly added and therefore doesn't have any previous value.
storageArea
Read only
-
Returns a Storage
object that represents the storage that was affected.
url
Read only
-
Returns string with the URL of the document whose key
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:
window.addEventListener("storage", () => {
console.log(JSON.parse(window.localStorage.getItem("sampleList")));
});
The same action can be achieved using the onstorage
event handler property:
window.onstorage = () => {
console.log(JSON.parse(window.localStorage.getItem("sampleList")));
};
Specifications
Browser compatibility
|
Desktop |
Mobile |
|
Chrome |
Edge |
Firefox |
Internet Explorer |
Opera |
Safari |
WebView Android |
Chrome Android |
Firefox for Android |
Opera Android |
Safari on IOS |
Samsung Internet |
storage_event |
1 |
15 |
45 |
9 |
15 |
4 |
≤37 |
18 |
45 |
14 |
4 |
1.0 |
See also