storage Event Fires In Other Tabs Only
The storage event reaches every same-origin page except the one that wrote, carrying key, oldValue, and newValue; a null key means another tab called clear.
Code
// the storage event fires only in the "other" same-origin tabs; the writing tab itself never receives it
window.addEventListener('storage', (event) => {
// key is null: another tab called clear()
if (event.key === null) {
console.log('storage cleared elsewhere');
return;
}
console.log('key:', event.key, event.oldValue, '->', event.newValue);
});
// a write from this tab never triggers its own listener
localStorage.setItem('theme', 'dark');
// when another tab writes the same key, this page receives:
// key: theme null -> dark
// oldValue/newValue are strings; objects must be JSON.parse-ed by handPoints of care
- The package manager never enters the picture for this snippet.
- A browser is the runtime here:
window,localStorageare not available in Node.
Putting it to use
Each step the file takes is announced in the output: storage cleared elsewhere, key:.
Core lines
window.addEventListener('storage', (event) => {
// key is null: another tab called clear()
if (event.key === null) {
console.log('storage cleared elsewhere');
return;
}
console.log('key:', event.key, event.oldValue, '->', event.newValue);
});
Interactive Tools for This Topic
DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.Dotenv File ParserParse .env file content into a JSON object, handling comments, export prefixes, single quotes, and double-quoted escape sequences.Indentation ConverterConvert leading indentation between tabs and spaces at a chosen width, leaving inner content untouched.INI File ParserParse INI configuration files into nested JSON with section objects, global keys, and both semicolon and hash comment support.
FAQ
- Does storage Event Fires In Other Tabs Only run in plain Node.js?
- No — it touches `window` and `localStorage`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.