Return to Snippet

Revision: 67737
at October 23, 2014 21:10 by burnandbass


Initial Code
//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;
    }

Initial URL


Initial Description
saveToStorage("objectName", {javascript:"object"});

To get the object use getFromStorage("objectName");

If the object does not exist, the function returns new empty object;

Initial Title
Add/Get objects From/To LocalStorage

Initial Tags
json

Initial Language
JavaScript