
30/10/2024
localStorage is a web storage solution provided by HTML5 that enables you to store key/value pairs in a web browser with no expiration time. It is particularly useful for creating applications that save user preferences, settings, and other data without requiring cookies or server-side storage. Below, we’ll explore how to use localStorage effectively.
localStorage works with the following fundamental properties:
You can perform several basic operations with localStorage: set an item, get an item, remove an item, and clear the storage. Here’s how these operations work:
Use localStorage.setItem(key, value) to store a value at the specified key. Both the key and value are strings.
localStorage.setItem('username', 'JohnDoe');
In this example, we set a username in localStorage with the key "username".
To retrieve a value, use localStorage.getItem(key). If the key does not exist, this method returns null.
let userName = localStorage.getItem('username'); console.log(userName); // Output: JohnDoe
Here, we retrieve the stored username from localStorage.
To delete a specific key/value pair, you can use localStorage.removeItem(key).
localStorage.removeItem('username');
This command will remove the username entry we previously added.
If you want to delete all stored data for that origin, use localStorage.clear().
localStorage.clear();
This will wipe all key/value pairs stored in localStorage, so use it carefully!
Imagine you are building a simple to-do application. You want to save the list of tasks so that the user doesn’t lose them upon refreshing the page.
Here’s how you can implement this:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>To-Do List</title> </head> <body> <input type="text" id="taskInput" placeholder="Add a new task" /> <button id="addTaskBtn">Add task</button> <ul id="taskList"></ul> <script> const taskInput = document.getElementById('taskInput'); const taskList = document.getElementById('taskList'); const addTaskBtn = document.getElementById('addTaskBtn'); // Load existing tasks from localStorage window.onload = function() { const tasks = JSON.parse(localStorage.getItem('tasks')) || []; tasks.forEach(task => addTaskToDOM(task)); }; addTaskBtn.onclick = function() { const task = taskInput.value; if (task) { addTaskToDOM(task); saveTask(task); taskInput.value = ''; // Clear input } }; function addTaskToDOM(task) { const li = document.createElement('li'); li.textContent = task; taskList.appendChild(li); } function saveTask(task) { const tasks = JSON.parse(localStorage.getItem('tasks')) || []; tasks.push(task); localStorage.setItem('tasks', JSON.stringify(tasks)); } </script> </body> </html>
Data Type: Remember that localStorage only accepts strings. If you need to store objects or arrays, convert them using JSON.stringify() before storing, and use JSON.parse() when retrieving.
Privacy and Security: Do not store sensitive information (like passwords) in localStorage, as it's accessible through JavaScript and can be vulnerable to XSS attacks.
Browser Support: localStorage is widely supported across modern browsers. It’s always a good idea to check for browser compatibility, especially for older versions.
By understanding and utilizing localStorage effectively, you can create smooth user experiences where data is retained, enhancing interaction and engagement with your web applications.
30/10/2024 | HTML5
30/10/2024 | HTML5
30/10/2024 | HTML5
30/10/2024 | HTML5
30/10/2024 | HTML5
30/10/2024 | HTML5
30/10/2024 | HTML5