1var testObject = { 'one': 1, 'two': 2, 'three': 3 };
2
3// Put the object into storage
4localStorage.setItem('testObject', JSON.stringify(testObject));
5
6// Retrieve the object from storage
7var retrievedObject = localStorage.getItem('testObject');
8
9console.log('retrievedObject: ', JSON.parse(retrievedObject));
1var person = { "name": "billy", "age": 23};
2
3localStorage.setItem('person', JSON.stringify(person)); //stringify object and store
4var retrievedPerson = JSON.parse(localStorage.getItem('person')); //retrieve the object
1// 'key' could be any variable to address your 'value'.
2//'value' could be of any data-type.
3
4// Save data to localStorage
5localStorage.setItem('key', JSON.stringify('value'));
6
7// Get saved data from localStorage
8localStorage.getItem('key') ? JSON.parse(localStorage.getItem('key') : '';
9
10// Remove saved data from localStorage
11localStorage.removeItem('key');
12
13// Remove all saved data from localStorage
14localStorage.clear();
1function createItem() {
2 localStorage.setItem('nameOfItem', 'value');
3}
4createItem() // Creates a item named 'nameOfItem' and stores a value of 'value'
5
6function getValue() {
7 return localStorage.getItem('nameOfItem');
8} // Gets the value of 'nameOfItem' and returns it
9console.log(getValue()); //'value';
1// Check browser support
2if (typeof(Storage) !== "undefined") {
3 // Store
4 localStorage.setItem("lastname", "Smith");
5 // Retrieve
6 document.getElementById("result").innerHTML = localStorage.getItem("lastname");
7} else {
8 document.getElementById("result").innerHTML = "Sorry, your browser does not support Web Storage...";
9}