Skip to content

Instantly share code, notes, and snippets.

@jfsiii
Forked from paulirish/gist:601751
Created February 7, 2011 17:33
Show Gist options
  • Save jfsiii/814784 to your computer and use it in GitHub Desktop.
Save jfsiii/814784 to your computer and use it in GitHub Desktop.
get and set objects in localStorage and sessionStorage
// Forked from paul irish's https://gist.github.com/601751
// desire: localStorage and sessionStorage should accept objects
// in fact they do according to spec.
// http://code.google.com/p/chromium/issues/detail?id=43666
// but so far that part isnt implemented anywhere.
// so we duckpunch set/getItem() to stringify/parse on the way in/out
// this seems to work in chrome
// but fails in FF (cant redefine setItem as a function, it just tostring's it)
// ^^^ Should be fixed now. Tested in FF 3.6 Mac OS X 10.6
// THIS IS INCOMPLETE CODE. would love any help if you'd like to help! :)
// Roger Raymond helped.
// John Schulz helped :)
(function(){
var toString = Object.prototype.toString,
isString = function ( v ) {
return (/^\[object String\]$/).test( toString.call(v) );
},
isObject = function ( v ) {
return (/^\[object Object\]$/).test( toString.call(v) );
};
// do a feature test to see if get/setItem natively support objects..
var nativeJSONStorage = (function () {
var feature_key = 'featureTest';
if (!window.sessionStorage) return false;
sessionStorage.setItem(feature_key,{});
var bool = isObject( sessionStorage.getItem(feature_key) );
sessionStorage.removeItem(feature_key);
return bool;
})();
if (nativeJSONStorage) return;
if (window.Storage){
var setter = Storage.prototype.setItem,
getter = Storage.prototype.getItem,
unique = (''+Math.random()).slice(-6) + 'json:';
Storage.prototype.setItem = function ( k, v )
{
var result;
// already a string. just set it
if ( isString( v ) ){
result = setter.apply(this, arguments);
} else {
// this logic to determine if we stringify could be stronger..
// might want to trycatch and only use json'd if it succeeds.
v = unique + JSON.stringify(v);
result = setter.call(this, k, v);
}
return result;
};
Storage.prototype.getItem = function ( k )
{
var result = getter.apply(this, arguments), str;
if (result.indexOf(unique) === 0){
str = result.slice( unique.length );
result = JSON.parse( str );
}
return result;
};
}
// TODO: do the whole thing over with sessionStorage. (done?)
// obv DRY that shit up.
})();
// tests
console.group('localStorage');
localStorage.setItem('storage', {local:true});
console.log(localStorage.getItem('storage') && localStorage.getItem('storage').local == true);
console.groupEnd('localStorage');
console.group('sessionStorage');
sessionStorage.setItem('storage', {session: true});
console.log(sessionStorage.getItem('storage') && sessionStorage.getItem('storage').session == true);
console.groupEnd('sessionStorage');
@Chofoteddy
Copy link

Take a look at my fork and let me know what you think 😃

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment