Skip to content

Instantly share code, notes, and snippets.

@rachelbaker
Created August 6, 2013 02:00
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rachelbaker/6161375 to your computer and use it in GitHub Desktop.
Save rachelbaker/6161375 to your computer and use it in GitHub Desktop.
Extending Local Storage to save and get Objects
/**
* Extending the Local Storage Object to allow saving of objects.
*
* @param int|string key object key
* @param int|string value object value
* @return bool true|false
*/
Storage.prototype.setObject = function(key, value) {
this.setItem(key, JSON.stringify(value));
};
/**
* Extending the Local Storage Object to allow returning of saved objects.
*
* @param int|string key object key
* @return int|string value
*/
Storage.prototype.getObject = function(key) {
var value = this.getItem(key);
return value && JSON.parse(value);
};
@xeoncross
Copy link

Doesn't handle exceptions like private browsing mode

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