HTML Web Storage Get Certified
What is HTML Web Storage?
The Web Storage API lets you store key-value pairs in the browser. There are two types:
- localStorage – Data stays even after the browser is closed.
- sessionStorage – Data is cleared when the tab/window is closed.
Unlike cookies, Web Storage data is not sent with every HTTP request, so it is more efficient for client-side data.
HTML Web Storage Code Examples
1. Basic localStorage Example
Store a value and retrieve it. The value remains after page reload.
What this code does: Saves a name in localStorage and shows it when the page loads or when you click the button.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>localStorage Example</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
input, button { padding: 10px; margin: 5px 0; font-size: 16px; }
#result { margin-top: 15px; font-size: 18px; }
</style>
</head>
<body>
<h2>localStorage Demo</h2>
<input type="text" id="nameInput" placeholder="Enter your name">
<button onclick="saveName()">Save Name</button>
<button onclick="clearName()">Clear</button>
<div id="result"></div>
<script>
// Show saved name when page loads
window.onload = function() {
const saved = localStorage.getItem("username");
if (saved) {
document.getElementById("result").innerHTML = "Hello, " + saved + "!";
}
};
function saveName() {
const name = document.getElementById("nameInput").value;
if (name) {
localStorage.setItem("username", name);
document.getElementById("result").innerHTML = "Hello, " + name + "! (Saved)";
}
}
function clearName() {
localStorage.removeItem("username");
document.getElementById("result").innerHTML = "Name cleared.";
document.getElementById("nameInput").value = "";
}
</script>
</body>
</html>
2. sessionStorage Example
Data stored in sessionStorage is available only for the current tab session.
What this code does: Saves a value that disappears when you close the tab.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>sessionStorage Example</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
input, button { padding: 10px; margin: 5px 0; font-size: 16px; }
#result { margin-top: 15px; font-size: 18px; }
</style>
</head>
<body>
<h2>sessionStorage Demo</h2>
<input type="text" id="noteInput" placeholder="Type a note">
<button onclick="saveNote()">Save Note</button>
<button onclick="showNote()">Show Note</button>
<div id="result"></div>
<script>
function saveNote() {
const note = document.getElementById("noteInput").value;
sessionStorage.setItem("tempNote", note);
document.getElementById("result").innerHTML = "Note saved for this session.";
}
function showNote() {
const note = sessionStorage.getItem("tempNote");
if (note) {
document.getElementById("result").innerHTML = "Saved note: " + note;
} else {
document.getElementById("result").innerHTML = "No note found (or session ended).";
}
}
</script>
</body>
</html>
3. Checking Support + Storing Objects
Always check if Web Storage is available, and use JSON for objects.
What this code does: Checks support, stores a simple object, and retrieves it.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Web Storage with Objects</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
button { padding: 10px 16px; margin: 5px; font-size: 16px; cursor: pointer; }
#output { margin-top: 15px; font-size: 16px; background: white; padding: 15px; border-radius: 6px; }
</style>
</head>
<body>
<h2>Store & Retrieve an Object</h2>
<button onclick="saveUser()">Save User Object</button>
<button onclick="loadUser()">Load User Object</button>
<button onclick="clearAll()">Clear Storage</button>
<div id="output">Click a button...</div>
<script>
function saveUser() {
if (typeof(Storage) === "undefined") {
document.getElementById("output").innerHTML = "Web Storage is not supported.";
return;
}
const user = {
name: "Alex",
age: 28,
city: "Delhi"
};
// Objects must be converted to string
localStorage.setItem("user", JSON.stringify(user));
document.getElementById("output").innerHTML = "User object saved!";
}
function loadUser() {
const data = localStorage.getItem("user");
if (data) {
const user = JSON.parse(data);
document.getElementById("output").innerHTML =
"Name: " + user.name + "<br>" +
"Age: " + user.age + "<br>" +
"City: " + user.city;
} else {
document.getElementById("output").innerHTML = "No user data found.";
}
}
function clearAll() {
localStorage.clear();
document.getElementById("output").innerHTML = "Storage cleared.";
}
</script>
</body>
</html>
Useful Methods
setItem(key, value)– Store datagetItem(key)– Retrieve dataremoveItem(key)– Delete one itemclear()– Delete everythingkey(index)– Get the key at a positionlength– Number of stored items
Web Storage vs Cookies
- Web Storage can hold much more data (usually 5–10 MB).
- Data is not automatically sent to the server.
- Cookies are limited to ~4 KB and are sent with every request.
Web Resources on HTML Web Storage
1. MDN : Web Storage API
2. MDN : Using the Web Storage API
Questions and Answers related to HTML Web Storage
localStorage keeps data even after the browser is closed. sessionStorage clears the data when the tab or window is closed.
Use localStorage.setItem("key", "value") to store and localStorage.getItem("key") to retrieve. The same methods work with sessionStorage.
Yes, but you must convert them to strings first with JSON.stringify() and convert them back with JSON.parse() when reading.
Most browsers allow about 5–10 MB per origin. Exceeding the limit throws a QuotaExceededError.
No. Data is accessible via JavaScript, so never store sensitive information (passwords, tokens, etc.) in localStorage or sessionStorage. It is vulnerable to XSS attacks.
🧪 Test Your HTML Code
Edit the HTML code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.