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.
Syntax
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.
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…
Code
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>
Open Try It Yourself in a second tab to see storage sync.
How It Works
setItem stores the theme for the origin.
This tab updates its label in the click handler.
Other tabs hear storage and call show(newValue).
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.
Compatibility
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 ChromeAll
Full support
Mozilla FirefoxAll
Full support
Apple SafariAll
Full support
Microsoft EdgeAll
Full support
OperaAll
Full support
Samsung InternetAll
Full support
Internet Explorer9+
Supported*
storageWidely supported
Bottom line: *Safe on modern browsers. Remember: the writing document does not receive storage.
Remember
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.