Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luflow/f5af897f9f0bddfbb095e4209d6086a6 to your computer and use it in GitHub Desktop.
Save luflow/f5af897f9f0bddfbb095e4209d6086a6 to your computer and use it in GitHub Desktop.
This is the best localStorage polyfill in the world
// I mean, seriously, localStorage is supported even by your mum. How about instead of
// casing the feature out, you give users in-memory (stale) storage instead?
// If they close your application, they deserve to lose data anyway.
if (!('localStorage' in window)) {
window.localStorage = {
_data : {},
setItem : function(id, val) { return this._data[id] = String(val); },
getItem : function(id) { return this._data.hasOwnProperty(id) ? this._data[id] : null; },
removeItem : function(id) { return delete this._data[id]; },
clear : function() { return this._data = {}; }
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment