Computer >> Computer tutorials >  >> Programming >> Javascript

Get data from sessionStorage in JavaScript?


sessionStorage

The localStorage and sessionStorage properties allow to save key/value pairs in a web browser. The sessionStorage object stores data for only one session. The data will be deleted when the browser is closed. It works the same as local storage. Initially, we have to check whether the browser supports the session storage or not. Later on, we have to create the data and have to retrieve the data. 

Syntax to set data in sessionStorage

sessionStorage.setItem();

Example

<html>
<body>
<p> id = "storage"</p>
<script>
   if (typeof(Storage) !== "undefined") {
      sessionStorage.setItem("product", "Tutorix");
      document.getElementById("storage").innerHTML = sessionStorage.getItem("product");
   }
   else {
      document.getElementById("storage").innerHTML = "Sorry,no Web Storage compatibility...";
   }
</script>
</body>
</html>

Output

Tutorix