HTML onstorage Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onstorage attribute handles the window storage event. It fires when another document that shares the same storage area updates localStorage or sessionStorage.

Put it on <body>, or use window.addEventListener("storage", …). The page that wrote the value does not receive the event — other same-origin tabs do (for localStorage). Prefer listeners over inline handlers.

Baseline · Widely available.

Stable across modern browsers since about April 2017. Not deprecated. Not cancelable; does not bubble.

Same-document writes do not fire storage.

Open a second tab (or use Simulate) to see the handler. This is by design, not a bug.

Inline handlers discouraged.

MDN: prefer addEventListener over inline on* attributes. Study onstorage to recognize it in markup.

HTML Syntax

syntax.html
<body onstorage=" script">

script runs when another document updates shared storage. Preferred: window.addEventListener("storage", handler). Use event.key, event.oldValue, event.newValue.

Attribute Values

How you listen for storage:

Value / formDescriptionTry it
JavaScript on bodyInline handler (study form)Try it
addEventListener("storage")Preferred; read key / valuesTry it
Cross-tab themeSync a setting via localStorageTry it

Live Preview

This page listens for storage. Write updates storage in this tab (no event here). Simulate fires a study event. For a real cross-tab test, open Try It Yourself #3 in two tabs.

action

Key: ctf-onstorage-demo. Same-tab writes never fire storage on this window.

Demo

Stored: (none)

Waiting for storage…

HTML Examples

Short study snippets. Open Try It Yourself in two tabs for a real event, or use Simulate on one tab.

Example 1 — Inline onstorage on body

Recognize the attribute form. Change storage from another tab to fire it.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>onstorage demo</title>
</head>
<body onstorage="document.getElementById('status').textContent = 'storage: ' + event.key + ' → ' + event.newValue">
  <p id="status">Open this page in two tabs. Write in one; watch the other.</p>
  <button type="button" onclick="localStorage.setItem('ctf-onstorage-demo', 'ping-' + Date.now())">Write</button>
</body>
</html>
Try It Yourself

How It Works

  1. onstorage on body registers a window-level handler.
  2. Write in tab A updates localStorage.
  3. Tab B (same origin) receives storage.
  4. Tab A does not — by design.

Example 2 — Preferred addEventListener

Log key, oldValue, and newValue from the StorageEvent.

HTML
<p id="status">Listening for storage…</p>
<button type="button" id="write">Write</button>
<button type="button" id="sim">Simulate</button>

<script>
  var status = document.getElementById("status");
  var KEY = "ctf-onstorage-demo";

  window.addEventListener("storage", function (event) {
    status.textContent =
      "key=" + event.key +
      ", old=" + event.oldValue +
      ", new=" + event.newValue;
  });

  document.getElementById("write").addEventListener("click", function () {
    localStorage.setItem(KEY, "value-" + Date.now());
    status.textContent = "Wrote in this tab (no storage event here).";
  });

  document.getElementById("sim").addEventListener("click", function () {
    window.dispatchEvent(new StorageEvent("storage", {
      key: KEY,
      oldValue: "before",
      newValue: "after",
      url: location.href,
      storageArea: localStorage
    }));
  });
</script>
Try It Yourself

How It Works

  1. Listener on window — no inline attribute.
  2. Write updates storage but skips this window’s event.
  3. Simulate builds a StorageEvent for same-tab study.
  4. Properties show what changed across documents.

Example 3 — Cross-tab theme sync

Open this example in two tabs. Change theme in one; the other updates.

HTML
<p id="theme" aria-live="polite">Theme: light</p>
<button type="button" data-theme="light">Light</button>
<button type="button" data-theme="dark">Dark</button>

<script>
  var KEY = "ctf-onstorage-theme";
  var label = document.getElementById("theme");

  function show(value) {
    label.textContent = "Theme: " + (value || "light");
  }

  show(localStorage.getItem(KEY));

  window.addEventListener("storage", function (event) {
    if (event.key === KEY) {
      show(event.newValue);
    }
  });

  var buttons = document.querySelectorAll("[data-theme]");
  for (var i = 0; i < buttons.length; i++) {
    buttons[i].addEventListener("click", function () {
      var value = this.getAttribute("data-theme");
      localStorage.setItem(KEY, value);
      show(value); // update this tab immediately
    });
  }
</script>
Try It Yourself

How It Works

  1. setItem stores the theme for the origin.
  2. This tab updates its label in the click handler.
  3. Other tabs hear storage and call show(newValue).
  4. Filter by key so unrelated storage noise is ignored.

Edge Cases & Pitfalls

Most “bugs” are expecting the writing tab to hear its own change.

same tab

Writer does not get storage

Update the UI in the click/setter path for the current document; use storage only for other documents.

session

sessionStorage is tab-scoped

Its storage events do not cross other tabs. Prefer localStorage for cross-tab demos.

origin

Same origin required

Different schemes/hosts/ports do not share storage and will not see each other’s events.

clear

clear() and removed keys

Removing a key sets newValue to null. Clearing may report key as null depending on the browser.

style

Prefer listeners

Inline on* attributes mix concerns. Use addEventListener in production code.

Browser Support

onstorage is a window event handler for Web Storage changes from other documents. Prefer addEventListener in apps.

✓ Baseline · Widely available

onstorage

Widely available since about April 2017. Chrome, Firefox, Safari, Edge, and Opera support the window storage event.

96% Global support (approx.)
Google Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer 9+
Supported*
storage Widely supported

Bottom line: *Safe on modern browsers. Remember: the writing document does not receive storage.

Key Takeaways

  • Role: hear storage changes from other documents.
  • Data: key, oldValue, newValue.
  • Cross-tab: use localStorage, not sessionStorage.
  • Writer tab: update UI yourself; no event there.

One line: onstorage syncs other tabs when localStorage changes — the writing tab must update itself.

Frequently Asked Questions

It is an event handler for the window storage event. That event fires when another document that shares the same Web Storage area updates localStorage or sessionStorage.
No. The window that made the change does not receive storage. Other same-origin tabs (for localStorage) do.
It is a window-level handler. In HTML, put it on body, or use window.addEventListener("storage", …) / window.onstorage.
A StorageEvent includes key, oldValue, newValue, url, and storageArea so you can see what changed.
localStorage changes notify other same-origin tabs. sessionStorage changes notify other browsing contexts in the same top-level tab (for example iframes), not other tabs.
Prefer window.addEventListener("storage", handler). MDN discourages inline HTML event handler attributes.

Next: onsubmit

Handle form submission with the submit event.

onsubmit tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful