Storing and Retrieve Value Using Session Storage in HTML5

Introduction

 
In this article, we will learn how to store a value in Session Storage and how to retrieve the stored value from the Session Storage using HTML5. HTML5 comes with the new feature Session Storage under Client Storage. HTML5 Session Storage is similar to HTTP session cookies. It is used for storing data on the client-side.
 
Session Storage simply stores the value on the basis of key/value pairs and Session Storage can store megabytes of values; the exact size depends on the browser implementation. For IE8 it is 10 MB. In HTML5 we can store the value of any element into a session variable and we can also fetch the stored value and display them on the webpage.
 
The Session Storage object does not persist if we close the tab. It is like a value stored into a variable and fetched when required. Session Storage works in IE8+, Firefox 3.5+, Safari 4.0+, Chrome 2.0+ & Opera 10.5+. The use of Session Storage is very smooth and easy. It is used like defining a variable and using it.
 
It is possible to store large amounts of data without affecting the website's performance. The data is stored in different areas for different websites, and a website can only access data stored by it. It is a global object in JavaScript. We can direct set and get values in this object using Session Storage methods. Session Storage should be used to store ephemeral data related to a single browser window as it doesn't persist after the window is closed.
 
The Session Storage basically consists of 4 main methods.
  • setItem(key, value): This method is used to set the value into the Session Storage based on the key.
  • getItem(key): This method is used to get the value that is stored into the Session Storage. It takes a key and returns the value.
  • removeItem(key): This method is used to remove a particular value from the Session Storage basis of the key.
  • clear(): This method is used to clear the Session Storage.
Here we use Session Storage to store the value and then, retrieve and display it using HTML5 and JavaScript.
 
Step 1:  HTML5 uses JavaScript to store and access the data.
 
Here is the code
  1. <script type="text/javascript">  
  2.         function dofirst() {  
  3.             var button = document.getElementById("button");  
  4.         button.addEventListener("click",savecrap,false);  
  5.         }  
  6.         function savecrap()  
  7.         {  
  8.         var one=document.getElementById("one").value;  
  9.                 var two=document.getElementById("two").value;  
  10.                 sessionStorage.setItem(one,two);  
  11.                 display(one);  
  12.         }  
  13.     function display(one)  
  14.     {  
  15.     var rightbox=document.getElementById("rightbox");  
  16.     var two=sessionStorage.getItem(one);  
  17.     rightbox.innerHTML="Name of variable:" +one+"<br/> value:" +two;  
  18.     }  
  19.     window.addEventListener("load",dofirst,false);  
  20.     </script> 
Step 2:  We add some CSS styling to code to dispaly the element well.
 
Here is the code:
  1. <style type="text/css">  
  2.         #one  
  3.         {  
  4.             width: 240px;  
  5.             background-color: #CCCCCC;  
  6.             font-family: Arial, Helvetica, sans-serif;  
  7.              font-size: large;  
  8.              color: #FF0000;  
  9.         }  
  10.         #two  
  11.         {  
  12.             width: 222px;  
  13.             font-family: Arial, Helvetica, sans-serif;  
  14.              font-size: medium;  
  15.               color: #FF0000;  
  16.               background-color: #CCCCCC;  
  17.         }  
  18.         #button  
  19.         {  
  20.             width: 116px;  
  21.             height: 39px;  
  22.         }  
  23.         #rightbox  
  24.         {  
  25.            width: 222px;  
  26.            height:200px;  
  27.            border: solid 2;  
  28.             font-family: Arial, Helvetica, sans-serif;  
  29.              font-size: medium;  
  30.               color: #FF0000;  
  31.               background-color: #CCCCCC;  
  32.         }  
  33.     </style> 
Step 3:  This is the code for defining the tag:
 
Here is the code:
  1. <body>  
  2. <section ud="leftbox">  
  3. <form>  
  4. <p>(key) One: <input type="text" id="one"</p>   
  5. <p>(value) Two: <textarea id="two">      <p></p>  
  6. <p> <input type="button" id="button" value="save"></p>  
  7.   
  8.   
  9. <section id="rightbox">  
  10. Nothing Yet Hoss!  
  11. </section> 
Step 4:  Write the full code into the .htm page:
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5.     <script type="text/javascript">  
  6.         function dofirst() {  
  7.             var button = document.getElementById("button");  
  8.         button.addEventListener("click",savecrap,false);  
  9.         }  
  10.         function savecrap()  
  11.         {  
  12.         var one=document.getElementById("one").value;  
  13.                 var two=document.getElementById("two").value;  
  14.                 sessionStorage.setItem(one,two);  
  15.                 display(one);  
  16.         }  
  17.     function display(one)  
  18.     {  
  19.     var rightbox=document.getElementById("rightbox");  
  20.     var two=sessionStorage.getItem(one);  
  21.     rightbox.innerHTML="Name of variable:" +one+"<br/> value:" +two;  
  22.     }  
  23.     window.addEventListener("load",dofirst,false);  
  24.     </script>  
  25.     <style type="text/css">  
  26.         #one  
  27.         {  
  28.             width: 240px;  
  29.             background-color: #CCCCCC;  
  30.             font-family: Arial, Helvetica, sans-serif;  
  31.              font-size: large;  
  32.              color: #FF0000;  
  33.         }  
  34.         #two  
  35.         {  
  36.             width: 222px;  
  37.             font-family: Arial, Helvetica, sans-serif;  
  38.              font-size: medium;  
  39.               color: #FF0000;  
  40.               background-color: #CCCCCC;  
  41.         }  
  42.         #button  
  43.         {  
  44.             width: 116px;  
  45.             height: 39px;  
  46.         }  
  47.         #rightbox  
  48.         {  
  49.            width: 222px;  
  50.            height:200px;  
  51.            border: solid 2;  
  52.             font-family: Arial, Helvetica, sans-serif;  
  53.              font-size: medium;  
  54.               color: #FF0000;  
  55.               background-color: #CCCCCC;  
  56.         }  
  57.     </style>  
  58. </head>  
  59. <body>  
  60. <section ud="leftbox">  
  61. <form>  
  62. <p>Enter the Key: <input type="text" id="one"</p>   
  63. <p>Enter the Value: <textarea id="two">      <p></p>  
  64. <p> <input type="button" id="button" value="save"></p>  
  65.   
  66. <section id="rightbox">  
  67. Session Storage is empty!!  
  68. </section> 
Output 1:  When there is no key\pair in the Session Storag
 
Output 2:  Output after saving the pair into the Session Storage
 
 
2.jpg