How to store a name permanently using HTML5 Local Storage?

HTMLWeb DevelopmentFront End Technology

The Local Storage is designed for storage that spans multiple windows and lasts beyond the current session. In particular, Web applications may wish to store megabytes of user data, such as entire user-authored documents or a user's mailbox, on the client side for performance reasons.

HTML5 localStorage saves string data in the browser and lasts beyond the current session. localStorage stores the data, with no expiration, whereas sessionStorage is limited to the session only. When the browser is closed, the session is lost.

The data won’t get deleted when the browser is closed. Here, we will save the name for example.

You can try to run the following code to learn how to store a name permanently using HTML5 local storage


Live Demo

<!DOCTYPE html>
      <div id = "demo"></div>
         if (typeof(Storage) !== "undefined") {
            localStorage.setItem("name", "John");
            document.getElementById("demo").innerHTML = localStorage.getItem("name");
         } else {
            document.getElementById("demo").innerHTML =
            "The browser do not support Web Storage";
Updated on 25-Feb-2020 07:00:54