Storing and retrieving objects with localStorage [HTML5]
Today I've played with this great feature of HTML5 for the first time. It was pretty straighforward and comfortable.
localStorage stores key-value pairs. So to store a entire javascript object we need to serialize it first (with JSON.stringify, for example):
localStorage.setItem('user', JSON.stringify(user));
Then to retrieve it from the store and convert to an object again:
var user = JSON.parse(localStorage.getItem('user'));
If we need to delete all entries of the store we can simply do:
localStorage.clear();
You can find JSON library to stringify and parse the objects here:
Written by Juan Fernández Sagasti
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#