CKTIS / HTML
onstorage — handling Web Storage changes
The onstorage attribute on body sets the Window handler for the storage event. It reacts to changes to shared localStorage or sessionStorage made in another document, rather than in the document performing the write.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
<body onstorage="handleStorage(event)">…</body>
window.onstorage = handleStorage;onstorage is an event handler attribute on body, not a global attribute. Its value contains JavaScript code with an available event parameter. It handles the Window object's storage event, not an event targeting body itself. Its JavaScript counterpart is window.onstorage; assigning a new function replaces the existing handler, and assigning null removes it.
An actual storage change asynchronously notifies other documents sharing that storage area, not the document performing the write. For localStorage, these can be other same-origin tabs. For sessionStorage, they must additionally belong to the same top-level browsing context, such as frames within the same tab. Writing an identical value does not trigger a notification.
The StorageEvent exposes key, oldValue, newValue, the changing document's url, and storageArea. A new key has an oldValue of null, a removed key has a newValue of null, and clear() sets all three data fields to null. An operation that changes nothing sends no notification.
The example dispatches a synthetic event without modifying storage to demonstrate the attribute deterministically. It does not test communication between documents. Running it requires JavaScript to be enabled and inline event handlers to be permitted.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>onstorage demo</title>
<body onstorage="document.getElementById('result').textContent = event.key + ': ' + event.oldValue + ' -> ' + event.newValue">
<pre id="result">Waiting</pre>
<script>
window.dispatchEvent(new StorageEvent('storage', {
key: 'theme',
oldValue: 'light',
newValue: 'dark'
}));
</script>
</body>
</html>Result
theme: light -> dark
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.