[フレーム]
Last Updated: February 25, 2016
·
2.938K
· steveniseki

Store JavaScript objects in HTML5 local storage

It is easy enough to pollyfill the localStorage object to store and retrieve objects rather than just strings.

if ( !Storage.prototype.setObject ) {
 Storage.prototype.setObject = function(key, value) {
 this.setItem(key, JSON.stringify(value));
 }
}

if ( !Storage.prototype.getObject ) {
 Storage.prototype.getObject = function(key) {
 var value = this.getItem(key);
 return value && JSON.parse(value);
 }
}

Now we can simply get and set objects from localStorage.

var apps = {
 appName : 'Links app',
 settings : {'type' : 'link'}
}; 

localStorage.setObject('apps', apps );

var ourApps = localStorage.getObject('apps');

AltStyle によって変換されたページ (->オリジナル) /