HTML onstorage Event Attribute


The onstorage HTML event attribute specifies a JavaScript function to be run when a Web Storage area is updated. Web Storage is a mechanism for storing data on the client side, and there are two types of Web Storage: session storage and local storage. Session storage is cleared when the browser window is closed, while local storage persists until the user explicitly clears it.

The onstorage event can be used to listen for changes to either session storage or local storage. It can be attached to the body element of an HTML document, or to the window object. When a change is detected, the onstorage event handler function is called.

The onstorage event handler function receives a single argument, which is an StorageEvent object. This object contains information about the change that occurred, such as the key of the changed item, the old value of the item, and the new value of the item.

Here is an example of how to use the onstorage event attribute:

<body onstorage="onStorageChange()">

function onStorageChange(event) {
  // Get the key of the changed item.
  var key = event.key;

  // Get the old value of the item.
  var oldValue = event.oldValue;

  // Get the new value of the item.
  var newValue = event.newValue;

  // Do something with the changed item.

The onstorage event can be used to implement a variety of features, such as:

  • Synchronizing data between multiple browser tabs or windows.
  • Saving and restoring user preferences.
  • Implementing real-time data updates.


<element onstorage="script">


  • scriptThe name of the script to use when the event has been triggered.


Currently no example.

Browser Support

The following table will show you the current browser support for the HTML onstorage Event Attribute.

Edge Chrome Firefox Opera Safari
Tablets / Mobile
Chrome Firefox Opera Safari Samsung Webview

Last updated by CSSPortal on: 14th October 2023