ewxn9g
Last Updated: February 25, 2016
·
7.523K
· jfsagasti
U1b6bd8ad071161fb5bd43535867f4590

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:

http://json.org/

Say Thanks
Respond