HTML Full Course [Day 15] [Hindi] π» | Local Storage APIsπ | Mohit Decodes
πΎ HTML Tutorial β Part 15: Local Storage APIs
Welcome to Day 15 of the HTML Full Course [Hindi] by Mohit Decodes! Today, we will learn about Local Storage APIs β a powerful way to store data directly in the userβs browser.
πΉ What is Local Storage?
Local Storage is a web storage API that lets you save key-value pairs in the browser with no expiration date. Unlike cookies, it can store more data and does not send data back to the server with every request.
βοΈ Basic Local Storage Methods
localStorage.setItem(key, value)
β Save datalocalStorage.getItem(key)
β Retrieve datalocalStorage.removeItem(key)
β Remove specific datalocalStorage.clear()
β Clear all stored data
π Example: Using Local Storage
html
CopyEdit
<!DOCTYPE html>
<html>
<head>
<title>Local Storage Demo</title>
</head>
<body>
<input type="text" id="nameInput" placeholder="Enter your name">
<button onclick="saveName()">Save Name</button>
<button onclick="showName()">Show Saved Name</button>
<p id="result"></p>
<script>
function saveName() {
const name = document.getElementById('nameInput').value;
localStorage.setItem('username', name);
alert('Name saved!');
}
function showName() {
const savedName = localStorage.getItem('username');
document.getElementById('result').innerText = savedName ? `Saved Name: ${savedName}` : 'No name found in storage.';
}
</script>
</body>
</html>
π‘ Use Cases for Local Storage:
- Saving user preferences
- Storing form data temporarily
- Caching small amounts of data for faster load
- Remembering login states (with care)