Skip to content

Instantly share code, notes, and snippets.

@atomrc
Created October 28, 2015 15:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atomrc/c22f699b898ef4dff7a6 to your computer and use it in GitHub Desktop.
Save atomrc/c22f699b898ef4dff7a6 to your computer and use it in GitHub Desktop.
Stupid fallback for localStorage when browser doesn't support it
/**
* Warning : only use this piece of code
* if storing things in the localStorage is
* NOT vital for your application to work
**/
/* function noop
* simply does nothing
**/
function noop() {}
/**
* if window.localStorage is defined then `storage`
* will contain it. Else `storage` will simply be a
* stupid mock for localStorage and won't store anything
*/
var storage = window.localStorage || {
length: 0,
key: noop,
setItem: noop,
getItem: noop,
removeItem: noop,
clear: noop
};
/**
* Then you can use `storage` like you would use `window.localStorage`
* without worrying about browser compatibility
* /
@ebourmalo
Copy link

Hey Thomas, agree with you on that :(
btw, for avoiding to declare an empty function, you can use directly Function.prototype :)

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