Working With Client Side Local Storage

Introduction

For many years, we developers were depending on the server only for saving the data. And we used to retrieve the data by sending a request to our server and by manipulating some queries we could manage the formation, updating, insertion and so on. Great, we were doing a fantastic job.

But as the day's passes, we must move on to the next level. Now a days all are busy in their life, so a person can wait a maximum of a few seconds for a request that he has given in your blog, or website, or in your product. If it takes much time, he/she will just close the window and start searching for an alternative. Am I right?

Yeah it happens when you are depending completely on the server. For each query it will take some seconds. Just because of this, we started using the client-side state management techniques, mostly cookies.

But we do have the following issues if we are using cookies also:

  • The cookies can have only a limited amount of data, which is definitely not enough for us to work on (4096 bytes).

  • And as the number of requests increases, the data amount in cookies also increase. So at a point of time, we may encounter some performance issues.

So in the modern web, we have a new solution, Local Storage.



Background


Currently I am working in a dashboard application. As you all know a dashboard application must be as fast as possible. It must not make a user wait. So most of our codes are in the client side. So we are storing the necessary data in the client side itself, that makes my app fast. And we are using jQuery.

Using the code

Now we will work on how to save the data in local storage and retrieve it when we need it.

Saving the Data

To save the data we do have a function called setItem(). We can use the following syntax.
  1. localStorage.setItem("item""http://sibeeshpassion.com/")  

Before going for this, you must check that your browser supports this feature, since this is only applicable to modern browsers.

To check whether it supports it or not:

  1. if (localStorage) {  
  2. }  
  3. else {  
  4. $("#showitem").text("your browser does not support this feature");  
  5. }  


Retrieving the data from local storage

We can use the getItem() function to retrieve the data from the local storage.

Please see the following syntax.
  1. var itm = localStorage.getItem("item")  
Great, you have it. Congratulations.

Now we will work on how to remove this item from local storage.

Removing data from the local storage

To remove the data form the local storage, please see the following syntax.
  1. localStorage.removeItem("item");  
Removing the all data from localStorage

To remove the data form the localStorage, please see the following syntax.

localStorage.clear();

Demo

Here we will create 3 buttons and one h2 tag as in the following markup.
  1. <input type="submit" id="setitem" value="set Item" />  
  2. <input type="submit" id="getitem" value="get Item" />  
  3. <input type="submit" id="removeitem" value="remove Item" />  
  4. <h2 id="showitem"></h2>  
Now we will add the jQuery reference.
  1. <script src="jquery-1.11.1.min.js"></script>  
Set the Item
  1. $("#setitem").click(function () {  
  2. localStorage.setItem("item""http://sibeeshpassion.com/")  
  3. });  
Once you set the item to localStorage , you can see it in your browser. To see it, press F12 after pressing the set Item button, then go to the resources tab.

Figure: Prior to setting the item

item1

Figure: After setting the item

item2

Get the Item
  1. $("#getitem").click(function () {  
  2.                     var itm = localStorage.getItem("item")  
  3.                     $("#showitem").text(itm);  
  4.                 });  
Remove the Item

 

  1. $("#removeitem").click(function () {  
  2.                     localStorage.removeItem("item");  
  3.                     $("#showitem").text('');  
  4.                 });  
Complete markup
  1. <!DOCTYPE html>  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title>Use Of Local Storage - Sibeesh|Passion</title>  
  5.     <script src="jquery-1.11.1.min.js"></script>  
  6.     <script>  
  7.         $(function () {  
  8.             if (localStorage) {  
  9.                 $("#setitem").click(function () {  
  10.                     localStorage.setItem("item""http://sibeeshpassion.com/")  
  11.                 });  
  12.                 $("#getitem").click(function () {  
  13.                     var itm = localStorage.getItem("item")  
  14.                     $("#showitem").text(itm);  
  15.                 });  
  16.                 $("#removeitem").click(function () {  
  17.                     localStorage.removeItem("item");  
  18.                     $("#showitem").text('');  
  19.                 });  
  20.             }  
  21.             else {  
  22.                 $("#showitem").text("your browser does not support this feature");  
  23.             }  
  24.         });  
  25. </script>  
  26. </head>  
  27. <body>  
  28.     <input type="submit" id="setitem" value="set Item" />  
  29.     <input type="submit" id="getitem" value="get Item" />  
  30.     <input type="submit" id="removeitem" value="remove Item" />  
  31.     <h2 id="showitem"></h2>  
  32. </body>  
  33. </html>  
That's all. We now have made our application fast and reliable . Download the source for the demo.

Kindest Regards

Sibeesh.

 

Up Next
    Ebook Download
    View all
    Learn
    View all