DEV Community

Cover image for HTML Tutorials: Introduction to HTML5 Web Storage #9
Zahir Hadi Athallah
Zahir Hadi Athallah

Posted on

HTML Tutorials: Introduction to HTML5 Web Storage #9

Introduction:

Welcome to Part 9 of our HTML tutorials! In this article, we will explore HTML5 Web Storage, a powerful feature that allows web applications to store data locally on the user's device. With Web Storage, you can persist data across sessions, enhance user experiences, and build responsive web applications. Let's dive into the world of HTML5 Web Storage!

What is HTML5 Web Storage?

HTML5 Web Storage provides a way for web applications to store data locally on the user's device. It consists of two mechanisms: localStorage and sessionStorage. Both mechanisms store key-value pairs, but they differ in terms of persistence and scope.

localStorage:

The localStorage mechanism allows you to store data that persists even when the browser is closed and reopened. The data stored in localStorage remains available until explicitly removed by the web application or cleared by the user.

Example:

// Storing data in localStorage
localStorage.setItem("username", "John");
localStorage.setItem("theme", "dark");

// Retrieving data from localStorage
var username = localStorage.getItem("username");
var theme = localStorage.getItem("theme");

console.log("Username: " + username);
console.log("Theme: " + theme);

// Removing data from localStorage
localStorage.removeItem("username");
Enter fullscreen mode Exit fullscreen mode

In the example above, we store the username and theme in localStorage. We can then retrieve the values using getItem() and remove the username using removeItem().

sessionStorage:

The sessionStorage mechanism allows you to store data that is available only within the current browser session. The data stored in sessionStorage is cleared when the browser session ends or is closed.

Example:

// Storing data in sessionStorage
sessionStorage.setItem("token", "abcd1234");
sessionStorage.setItem("isLoggedIn", "true");

// Retrieving data from sessionStorage
var token = sessionStorage.getItem("token");
var isLoggedIn = sessionStorage.getItem("isLoggedIn");

console.log("Token: " + token);
console.log("Is Logged In: " + isLoggedIn);

// Removing data from sessionStorage
sessionStorage.removeItem("token");
Enter fullscreen mode Exit fullscreen mode

In the example above, we store the authentication token and login status in sessionStorage. We can then retrieve the values using getItem() and remove the token using removeItem().

Limitations and Considerations:

  • Both localStorage and sessionStorage have a maximum storage limit, typically around 5MB per origin.

  • The data stored in Web Storage is specific to the origin (i.e., domain, protocol, and port). Data from one origin cannot be accessed by another.

  • The data stored in Web Storage is accessible to JavaScript running within the same origin, so be cautious about storing sensitive information.

Working with Objects:

Web Storage only supports storing data in string format. If you want to store complex data structures like objects or arrays, you need to convert them to a string format using JSON.stringify() when storing and JSON.parse() when retrieving.

Example:

var user = {
    name: "John",
    age: 30
};

localStorage.setItem("user", JSON.stringify(user));

var storedUser = JSON.parse(localStorage.getItem("user"));
console.log(storedUser.name); // Output: John
Enter fullscreen mode Exit fullscreen mode

In the example above, we convert the user object to a string using JSON.stringify() before storing it in localStorage. When retrieving, we parse the stored value using JSON.parse() to convert it back to an object.

Closing:

HTML5 Web Storage provides a convenient and efficient way to store data locally within web applications. In this tutorial, we explored the localStorage and sessionStorage mechanisms, learned how to store, retrieve, and remove data, and considered the limitations and best practices when working with Web Storage. By leveraging Web Storage, you can create responsive and personalized web experiences for your users. Happy coding!

Top comments (0)