NEW

6/recent/ticker-posts

Header Ads Widget

HTML Web Storage

 

HTML Web Storage

The Web Storage is one of the great features of HTML5. With the Web Storage feature, web applications can locally store data within the browser on the client side. It stores data in the form of key/value pair on the browser. Web Storage sometimes also known as DOM storage.

Storing data with the help of web storage is similar to cookies, but it is better and faster than cookies storage.

In compared to cookies Web Storage has Following Advantages:

  • Web Storage can use storage space upto 5MB per domain. (The browser software may prompt the user if the space limit is reached).
  • It will not send data to the server side, hence it is faster than cookies storage.
  • The data stored by local Storage never expires, but cookies data expires after some time or session.
  • Web Storage is more secure than cookies.

Types of Web Storage

There are two types of web storage with different scope and lifetime.

  • Local Storage: Local Storages uses Windows.localStaorage object which stores data and available for every page. But data persist even if the browser is closed and reopened (Stores data with no Expiration).
  • Session Storage: Session Storage uses Windows.sessionStorage object which stores data for one session and data will be lost if the window or browser tab will be closed.

Note: For both storage type, web storage data will not be available for different browsers, and Storage size may vary from browser to browser.

Browser support for Web Storage

Before learning for web Storage we must check whether our browser is supporting the web Storage or not. So you can check by executing the following code:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <body>  
  4.  <div id="result"></div>  
  5.  <script>  
  6.   if(typeof(Storage)!=="undefined") {  
  7.    document.getElementById("result").innerHTML = "Hey, Your browser supports the Web Storage.";  
  8.   }  
  9.   else{  
  10. document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage";  
  11.   }  
  12. </script>  
  13. </body>  
  14. </html>  

The localStorage Object

The localStorage object stores data locally within the browser. The data stored by localStroage object does not have any expiration date. Hence the stored data will not be deleted if the browser is closed or reopened.

Each piece of data is stored in simple key-value pairs. The key/values are always stored as String, and can be accessed with localStorage.getItem() and localStorage.setItem() methods.

Example:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.   <title>Web Storage API</title>  
  5.   <style>  
  6.     body{  
  7.       color: green;  
  8.       text-align: center;  
  9.       font-size: 30px;  
  10.       margin-top: 30px;  
  11.       font-style: italic;  
  12.     }  
  13.   </style>  
  14. </head>  
  15. <body>  
  16. <script>  
  17.  if(typeof(Storage)!=="undefined") {  
  18.   localStorage.setItem("name","Harshita");  
  19.   localStorage.setItem("Country", "India");  
  20.    document.write("Hi"+" "+localStorage.name+" "+"from" +" "+ localStorage.Country);  
  21. }  
  22.  else{  
  23.   alert("Sorry! your browser is not supporting the browser")  
  24.  }  
  25. </script>  
  26. </body>  
  27. </html>  

Example Explanation:

  • In the above example, we have used typeof(Storage)!=="undefined" to check browser support.
  • localStorage.setItem("name","Harshita") is used to set the key and value data where "name" is key and "Harshita" is value.
  • The localStorage.name is used to retrieve the values using key. You can also use another method:
    localStorage.getItem to retrieve the value.

Note: You can check the local storage items in the form of key/value pair by inspecting elements on the web page and then go to the Application option where you will find the local storage and Session storage and can check stored items in the list.

You can check the following screenshot with key/value pairs.

HTML Web Storage

Example 2:

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>   
  4.   <style>  
  5.    div{  
  6.     background-color: pink;  
  7.     height: 50px;  
  8.     }  
  9.   </style>  
  10. </head>  
  11. <body>  
  12.   <h2>Example of counter Using Local Storage</h2>  
  13.   <button onclick="counter();">click me</button>  
  14.   <div id="output">See the result here :</div>  
  15.   <script type="text/javascript">  
  16.     function counter() {  
  17.      if(localStorage.hits){  
  18.     localStorage.hits=Number(localStorage.hits)+1;  
  19.    }  
  20.  else{  
  21.   localStorage.hits=1;  
  22.  }  
  23.  document.getElementById('output').innerHTML"You have clicked counter button for"+ " "+ localStorage.hits +" "+"times";  
  24. }  
  25.   </script>  
  26.  <p>click the counter button to see the total counts. </p>  
  27.  <p>If you will close the browser still it will not reset. </p>  
  28. </body>  
  29. </html>  

Example Explanation:

In the above example, we have shown a counter which will increase as you will click on the counter button.

We have used localStorage.hits to set a counter

Note: It will show the total number of count even if you close the browser.

The sessionStorage Object:

The sessionStorage object is same as the localStorage object, but the difference is that it stores data only for one session. If you close the browser, then data will be lost or deleted.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>   
  4.   <style>  
  5.     div{  
  6.       background-color: pink;  
  7.       height: 50px;  
  8.     }  
  9.   </style>  
  10. </head>  
  11. <body>  
  12.   <h2>Example of counter Using Session Storage</h2>  
  13.   <button onclick="counter();">click me</button>  
  14.   <div id="output">See the result here:</div>  
  15.   <script type="text/javascript">  
  16.     function counter() {  
  17.      if(sessionStorage.hits){  
  18.       sessionStorage.hits=Number(sessionStorage.hits)+1;  
  19. }  
  20.  else{  
  21.   sessionStorage.hits=1;  
  22.  }  
  23.  document.getElementById('output').innerHTML"You have clicked counter button for"+ " "+ sessionStorage.hits +" "+"times";  
  24. }  
  25.   </script>  
  26.  <p>Click the counter button to see the total counts. </p>  
  27.  <p>Now, if you close the browser then it will reset to initial value. </p>  
  28. </body>  
  29. </html>  

Example Explanation:

The above example is working same as local storage counter example, but the difference is we have used sessionStorage.hits for session storage.

Here the counter will reset if you close the browser and it will start from the initial value.

Tips: You can make these examples more attractive and useful by using jQuery with JavaScript.

Remove Web Storage:

As we have seen the session storage data will automatically be deleted, when you close the browser but the data saved by local storage will remain in the browser even if you close it.

Hence to delete the local storage data, you need to call two methods:

  • localStorage.removeItem('key'): If you want to delete the value on a particular key, then you can use the "key," and that value will be deleted.
  • localStorage.clear(): If you want to delete or clear all settings with key/value pair, then you can call this method.

Example

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4.   <title>Web Storage API</title>  
  5.   <style>  
  6.      body{  
  7.       color: green;  
  8.       text-align: center;  
  9.       font-size: 30px;  
  10.       margin-top: 30px;  
  11.       font-style: italic;  
  12.       }  
  13.   </style>  
  14. </head>  
  15. <body>  
  16. <button onclick="remove();">Remove item</button>  
  17. <div id="output"></div>  
  18.   
  19. <script>  
  20.  if(typeof(Storage)!=="undefined") {  
  21.   localStorage.setItem("name","Harshita");  
  22.   localStorage.setItem("Country", "India");  
  23.   document.getElementById('output').innerHTML"Hii, my name is"+ " "+ localStorage.name +" "+"and i belongs to"+" "+localStorage.Country;  
  24.    }  
  25.   else{  
  26.   alert("Sorry! your browser is not supporting the browser")  
  27.  }  
  28.   function remove() {  
  29.  localStorage.removeItem("name");  
  30.    document.getElementById('output').innerHTML"Hii, my name is"+ " "+ localStorage.name +" "+"and i belongs to"+" "+localStorage.Country;  
  31. }  
  32. </script>  
  33. </body>  
  34. </html>  

Example Explanation:

In the above example we have used localStorage.removeItem("name"); Which will delete the value for the key "name".

You can remove id for a particular key, or you can also remove all data using localStorage.clear() method.

Post a Comment

0 Comments

Recents Post

Exploring Paidwork - Your Gateway to Earning Online