HTML5 Local Storage(Web Storage) mechanisms provides you the facility to store data much more easy way than cookies. Local storage save the data more secure then cookies, where cookie only can store data up to 5mb, local storage capacity is far larger than cookie.
In local storage you can set data with no expiration date, so if you close your browser and come next time your data will not be deleted.
Local data storage data is saved in key/value pair.
// Store data
localStorage.setItem("it_company", "Evon");
Lets try an example to save JSON array in local storage and receive the object from local storage.
var myObject = { 'color': 'red', 'name': 'prabhat', 'city': 'dehradun' };
// Put the object into storage
localStorage.setItem('savedObject', JSON.stringify(myObject)); //save data in local storage
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('savedObject'); //retrieve data from local storage
console.log('retrievedObject: ', JSON.parse(retrievedObject));
0 Comment(s)