User Id :    Password :      New Member   Forgot Password  
 
Store data at browser using HTML5 Web Storage
Description Store data at browser using HTML5 Web Storage   No. of Views     1171
  Rating     0
Author Pranay Rana   Posted On     10 Jun 2013
Tags HTML    

Sample Code   Download Code

 

Now new version of HTML5 having number of new API one of the is Storage API, which allow you to store data of use locally. Here in following post I am going to describe Storage API.

Cookies vs. Storage
In previous version of HTML allows to store data locally with the help of Cookies but the

     issue with the cookies is its not allow to big object and which can be easily done. Storage allows 5M (most browsers) or 10M (IE) of memory at disposal.
    Another problem is cookies get sent to server with each HTTP request which in term increases traffic.

Storage
Now lets start using the store in application

 

alert("storage is supported and you can store data in it");
}
else
{
 alert("Get new version of browser or use browser which support storage");
}



So here in above as you can see, first line of code check weather browser supports/have Storage object. It's good to check because most of the older browser is not supporting and as its new feature its mostly supported in new browsers.

After doing check for Storage support decide either you want to store data for given session only or want to store data which available even after session is over and when user come back.
So there are two type of object which is explained below

sessionStorage is used to store within the browser tab or window session. so it allows to store data in a single web page session.
localStorage is used to kept even between browser sessions. so it allows to access data after the browser is closed and reopened again, and also instantly between tabs and windows.

Note : Storage data created by one browser is not avaible in other browser. for example created in IE is not available in FireFox.

Following is the way you can use the localStorage and sessionStorage in your application.

//sessionStorage

//set and get object
sessionStorage.setItem('myKey', 'myValue');
var myVar = sessionStorage.getItem('myKey');
//another way to set and get
sessionStorage.myKey = 'myValue';
var myVar = sessionStorage.myKey;
 
//remove item
sessionStorage.removeItem('myKey');
 
//clear storage
sessionStorage.clear();

//localStorage
//set and get object
localStorage.setItem('myKey', 'myValue');
var myVar = localStorage.getItem('myKey');
//another way to set and get
localStorage.myKey = 'myValue';
var myVar = localStorage.myKey;
 
//remove item
localStorage.removeItem('myKey');
 
//clear storage
localStorage.clear();

as in the above code both of the object support same set of methods to store and retrieve data.

    setItem - allows to set value.
    getItem - allows to get value.
    removeItem - allows to remove object from storage. Note: if it used like removeItem(), it removes all stored object , so be careful when removing -to remove specific object use removeItem("myKey").
    clear - clear storage i.e. clear all stored data.

and as you can see storage store data in key value pair.

Conclusion
Web Storage simplify the storing of object in client and also have advantage over cookies, but its always good to not store sensitive information in the client side storage as storage is not provide that much security.
 

About Author

About Author Hey, I am Pranay Rana, working as a Senior Software engineer. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5 years now.
For me def. of programming is : Programming is something that you do once and that get used by multiple for many years
You can visit me on my blog - http://pranayamr.blogspot.com/
Pranay Rana
No Photo
 
Country India
Company GMind Solutions
Home Page http://pranayamr.blogspot.com/

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

   
Write your comment here.
Comment
Verification Code