How to store a name permanently using HTML5 Local Storage?


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

Example

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <div id = "demo"></div>
      <script>
         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";
         }
        </script>
   </body>
</html>

Updated on: 25-Feb-2020

292 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements