Using JSON objects for local storage

When it comes to Local Storage i recommend using an abstraction library for many of the features discussed here as well as better compatibility.

But sometimes i just love to use the best and easy approach, cos i think importing a library for something you may only use once or may end up not using up to 10% ability of the library.

What is HTML Local Storage?

With local storage, web applications can store data locally within the user’s browser. Before HTML5, application data had to be stored in cookies, included in every server request. Local storage is more secure, and large amounts of data can be stored locally, without affecting website performance. Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server.

Local storage is per origin (per domain and protocol). All pages, from one origin, can store and access the same data. more info in w3schools

SET local storage

var userData = {name:'Olatunde Owokoniran', job-title:'Software developer'};
localStorage.setItem('user', JSON.stringify(userData));

GET local storage

var localStorageData =JSON.parse(localStorage.getItem('user'));
console.log(localStorageData.name); //Olatunde Owokoniran
console.log(localStorageData.job-title); //Software developer

Iteration of all local storage keys and values

for (var i = 0, len = localStorage.length; i < len; ++i) {
  console.log(localStorage.getItem(localStorage.key(i)));
}

DELETE local storage

localStorage.removeItem('user');
delete window.localStorage["user"];

Read more:

Olatunde Owokoniran

Creating the world of ME with each code

Lagos, Nigeria hurlatunde.github.io