Session Storage is the browser’s sessionStorage object from the Web Storage API. From a <script> in your HTML page you save string key-value pairs for the current tab. Data survives reloads of that HTML document and is cleared when the tab or window closes.
How does a page session work?
Loading a document in a tab creates a page session for that tab. The main page and same-origin iframes share that tab’s storage. A new tab gets a new session (unlike session cookies). Closing the tab ends the session and deletes the data.
How is this useful in HTML pages?
Autosave draft text in an <input> or <textarea> across an accidental refresh, remember a wizard step for this visit only, or keep a temporary cart for one checkout tab—without sending data to a server.
When should you use sessionStorage vs localStorage?
Use sessionStorage for tab-scoped, temporary state. Use localStorage for settings that should persist across visits and be shared by every tab of the same origin.
Strings only.
Keys and values are strings. Store objects with JSON.stringify / JSON.parse.
Not a vault.
Same-origin scripts can read sessionStorage. Never store passwords or payment secrets there.
Opener note.
A page opened with window.open may start with a copy of the opener’s sessionStorage, but the two stores stay separate afterward.
Syntax
HTML / Script Syntax
Call methods on sessionStorage from a script in your HTML document:
JS
sessionStorage.setItem("key", "value");
var data = sessionStorage.getItem("key"); // string or null
sessionStorage.removeItem("key");
sessionStorage.clear(); // remove all keys for this tab
sessionStorage.key(0); // name of key at index
sessionStorage.length; // number of keys
HTML
<input id="field" type="text">
<script>
var field = document.getElementById("field");
if (sessionStorage.getItem("autosave")) {
field.value = sessionStorage.getItem("autosave");
}
field.addEventListener("change", function () {
sessionStorage.setItem("autosave", field.value);
});
</script>
Reference
Core HTML Concepts
How session storage fits into HTML pages and browser tabs:
Concept
Meaning
sessionStorage
Per-origin, per-tab Storage object on window
Page session
Lasts while the tab is open; survives HTML reloads
setItem / getItem
Write and read string values
removeItem / clear
Delete one key or all keys in this tab
key / length
Inspect stored keys for debugging
Same origin
Scheme + host + port must match to share storage
New tab
Starts a new empty sessionStorage (separate from other tabs)
localStorage
Persistent, shared across tabs of the same origin
sessionStorage vs localStorage
sessionStorage
localStorage
Scope
One tab (top-level browsing context)
All tabs of the origin
Lifetime
Until tab/window closes
Until cleared by script or user
Survives reload?
Yes
Yes
Best for
Drafts, wizards, one-visit state
Theme, prefs, lasting data
Approach
How to Use Session Storage in HTML
Wire storage to real form controls and page load.
Add HTML controls (input, textarea, buttons) with ids.
On save or change, call sessionStorage.setItem(key, value).
On page load (script at end of body or DOMContentLoaded), getItem and restore into the control.
Use removeItem when a step is finished, or clear to wipe the tab session.
Serialize objects with JSON before storing; parse carefully when reading.
HTML provides the UI; the script talks to sessionStorage.
Save writes the input value under userName.
Auto-restore on load fills the field after a refresh.
Close the tab and reopen—the saved name is gone.
Edge Cases & Pitfalls
Most surprises come from tab scope, string-only values, or blocked storage.
tabs
Expecting data in a new tab
Opening the same HTML URL in another tab starts a new sessionStorage. Duplicate data only if you copy it yourself (or use localStorage).
types
Storing numbers/objects raw
Non-strings are coerced. Prefer explicit String(…) or JSON so you do not get "[object Object]".
null
Forgetting null checks
getItem returns null when missing. Guard before writing into HTML inputs.
quota
QuotaExceededError
Very large strings can throw. Catch errors around setItem for big drafts.
file
file: / blocked cookies
Some environments throw SecurityError. Test over http(s). Privacy modes that block site data also block storage.
secrets
Sensitive data in HTML apps
XSS can read sessionStorage. Keep tokens and PII on the server with proper auth cookies when possible.
Compatibility
Browser Support
sessionStorage is part of the Web Storage API and is supported in all modern browsers.
✓ Baseline · Widely available
sessionStorage in HTML pages
Per-tab Web Storage works across current browsers for same-origin HTML documents.
100%sessionStorage support
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
Web Storage sessionStorageUniversal
Bottom line: Use sessionStorage for tab-scoped drafts—support is widespread; watch privacy settings and file: origins.
Remember
Key Takeaways
Per tab: sessionStorage is scoped to one browsing context, not every tab.
Survives reload of the HTML page; clears when the tab closes.
API:setItem, getItem, removeItem, clear.
Wire to HTML inputs so drafts restore after refresh.
One line: From your HTML page’s script, use sessionStorage for temporary, tab-only key-value data that outlives a refresh.
Frequently Asked Questions
sessionStorage is a Web Storage API on the window object. From a script in your HTML page you store string key-value pairs for the current browser tab. Data survives reloads of that page but is cleared when the tab or window closes.
localStorage is shared across tabs of the same origin and lasts until cleared. sessionStorage is partitioned by origin and by tab: each tab has its own store for the page session.
Yes. Reloading the same tab keeps sessionStorage. Opening the same HTML URL in a new tab starts a new, empty session for that tab.
On input or change events, call sessionStorage.setItem(key, value). On page load, read with getItem and write the value back into the form control so users do not lose draft text.
Only strings are stored. Use JSON.stringify before setItem and JSON.parse after getItem when you need objects.
Some origins (for example file: pages) or browser privacy settings that block site data can prevent Web Storage. Prefer testing over http(s) and avoid storing secrets—storage is readable by same-origin scripts.