/ Published in: JavaScript
saveToStorage("objectName", {javascript:"object"});
To get the object use getFromStorage("objectName");
If the object does not exist, the function returns new empty object;
To get the object use getFromStorage("objectName");
If the object does not exist, the function returns new empty object;
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//Save object function saveToStorage(_name, _obj) { console.log("saved to storage", _name, _obj); localStorage.setItem(_name, JSON.stringify(_obj)); } //Get the object, or a new one function getFromStorage(_name) { var storageData = localStorage.getItem(_name); var storageObj = JSON.parse(storageData); if (!storageObj) { storageObj = {}; saveToStorage(_name, storageObj); } return storageObj; }