Window.localStorage
Local Storage And How
To Use It On Websites
Storing information locally on a user’s computer is a
powerful strategy for a developer who is creating something for the Web. In
this article, we’ll look at how easy it is to store information on a computer to
read later and explain what you can use that for.
Adding State To The
Web: The “Why” Of Local Storage Link
The main problem with HTTP as the main transport layer of
the Web is that it is stateless. This means that when you use an application
and then close it, its state will be reset the next time you open it. If you
close an application on your desktop and re-open it, its most recent state is
restored.
This is why, as a developer, you need to store the state of
your interface somewhere. Normally, this is done server-side, and you would
check the user name to know which state to revert to. But what if you don’t
want to force people to sign up?
This is where local storage comes in. You would keep a key
on the user’s computer and read it out when the user returns.
Using Local Storage
In HTML5-Capable Browsers
Using local storage in modern browsers is ridiculously easy.
All you have to do is modify the localStorage object in JavaScript. You can do
that directly or (and this is probably cleaner) use the setItem() and getItem()
method:
localStorage.setItem('favoriteflavor','vanilla');
You can see this data in your browser local storage
You can see this data in your browser local storage
If you read out the favoriteflavor key, you will get back
“vanilla”:
var taste =
localStorage.getItem('favoriteflavor');
// -> "vanilla"
To remove the item, you can use — can you guess? — the
removeItem() method:
localStorage.removeItem('favoriteflavor');
var taste =
localStorage.getItem('favoriteflavor');
// -> null
That’s it! You can also use sessionStorage instead of localStorage
if you want the data to be maintained only until the browser window closes.
Limitation of localstorage
http://www.sitepoint.com/html5-local-storage-revisited/
Download a todo application using localstorage. Click here
Download a todo application using localstorage. Click here
0 comments:
Post a Comment