HTML Tutorial
HTML Web Storage
localStorage and sessionStorage keep small strings in the browser between visits.
A tiny notebook in the browser
Web Storage lets JavaScript save small pieces of text on the visitor’s device. No server and no cookie header: you call setItem and later getItem. The data stays in that browser, for that site.
Two stores exist. localStorage lasts until the user (or your script) clears it.sessionStorage lasts until the tab closes.
setItem and getItem
Both stores use a key and a value. Keys and values are strings.
Example
<script>
localStorage.setItem('theme', 'dark');
const theme = localStorage.getItem('theme');
document.body.textContent = 'Saved theme: ' + theme;
</script>getItem returns null if the key was never set. Check before you use the value. removeItem('theme') deletes one key; clear() deletes all keys for that store on this site.
Run storage examples in /html/try, the HTML live preview. The preview executes scripts, so setItem can run there.
localStorage versus sessionStorage
| localStorage | sessionStorage | |
|---|---|---|
| Survives closing the tab | Yes | No |
| Shared across tabs on the same site | Yes | No (per tab) |
| API | setItem / getItem | The same |
| Good for | A saved name, a theme choice | A step in a wizard |
Same methods, different lifetime. If you want the value back next week, uselocalStorage. If it should vanish when the tab closes, usesessionStorage.
Only strings
Web Storage does not store numbers, objects, or arrays as themselves. Anything you pass tosetItem is turned into a string. A number becomes "42". An object becomes the useless string "[object Object]" unless you convert it first.
For a single name or theme, a plain string is enough. For structured data, people often useJSON.stringify on the way in and JSON.parse on the way out. Stay with one string while you learn the API.
Capacity is small — a few megabytes per site, often less. It is not a database. Keep values short.
Not for passwords
Anything in Web Storage is readable by JavaScript on that site. If a script injection bug ever runs, it can call getItem and send the values away. Do not store passwords, session tokens you would treat as login, or private documents.
Cookies marked HttpOnly stay hidden from scripts; Web Storage never does. Use storage for preferences: a display name, a color theme, “hide this banner.” Keep secrets on the server.
StudyGrid’s preview may run in a sandbox or a different origin than your published site. Values you save in the editor might not appear on the real domain, and the reverse is also true. Treat preview storage as a demo, not as production data.
Save a name from an input
This page stores whatever you type, then fills the field again on the next load. Keep the script short: read the input, setItem on click, getItem on load.
Example
<label for="name">Your name</label>
<input id="name" type="text" autocomplete="name">
<button id="save" type="button">Save</button>
<p id="status"></p>
<script>
const input = document.getElementById('name');
const saved = localStorage.getItem('demo-name');
if (saved) input.value = saved;
document.getElementById('save').onclick = function () {
localStorage.setItem('demo-name', input.value);
document.getElementById('status').textContent = 'Saved.';
};
</script>Click Save, then refresh the preview. The name should still be there if this origin allows storage. If it vanishes, the sandbox isolated storage — try the same file opened as a normal local HTML page.
What you should remember
localStoragepersists;sessionStoragedies with the tab- Both store strings only
- Never put passwords in either store
- A preview or iframe sandbox may keep a separate, empty store
You have reached the end of this HTML track’s graphics and media chapters. Go back through canvas, SVG, and the media tags whenever a page needs a drawing or a clip instead of only text.
Worked examples
The short listings above show the tag in isolation. These pages use the same markup on documents you would actually publish: a lab report, a clinic form, a timetable, a weather card.
HTML does not calculate. It names the pieces so a browser, a screen reader, and a search engine can tell a heading from a paragraph. The numbers below are classroom values — the same Ohm, pH, and pulse figures as the C track — now sitting in real page structure.
Preview them in the HTML editor at /html/try. Change a heading or a number and watch the page, not a print log.
Engineering
Remember the last reading
localStorage keeps strings until the user clears site data. sessionStorage lasts for the tab. Neither is a database, and neither is private from scripts on this origin.
JSON.stringify objects before you store them. Always setItem/getItem with the same key. This is how a logger “remembers” 12 mT after refresh.
Example
<button id="save" type="button">Remember 12 mT</button>
<p id="out"></p>
<script>
document.getElementById("save").onclick = function () {
localStorage.setItem("field", "0.012");
document.getElementById("out").textContent =
"stored " + localStorage.getItem("field") + " T";
};
</script>Biology
A seat number for this visit
sessionStorage is the waiting-room ticket: gone when the tab closes. Do not store health records here. It is a convenience cache, not a hospital system.
Example
<script>
sessionStorage.setItem("seat", "12");
document.body.insertAdjacentHTML(
"beforeend",
"<p>Seat " + sessionStorage.getItem("seat") + "</p>"
);
</script>