Skip to content

localStorage

pierr edited this page Nov 16, 2014 · 6 revisions

LocalStorage

Here is a detail article of mozilla on it.

Vanilla JS (standard js)

localstorage.setItem("key", JSON.stringify({firstName: "Pierre", lastName: "Besson"})); //Save the object in the localStorage.
localstorage.getItem("key") // return 
//In order to have the real object

JSON.parse(localstorage.getItem("key")); //Return {firstName: "Pierre", lastName: "Besson"}

localforage

In order to be more efficient with local storage and also provide a way to use other storage which will be available in HTML5 such as indexedDB we will use a library provided by Mozilla which is localforage.

The syntax is almost the same as for the local storage but each operation is asynchronous. Save an object.

localforage.setItem("key", {firstName: "Pierre", lastName: "Besson"}); //Save the object in the local storage
Clone this wiki locally