Skip to content

Instantly share code, notes, and snippets.

@antoine-pous
Created November 12, 2015 12:12
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 antoine-pous/c73e5a3a3b1e9507597d to your computer and use it in GitHub Desktop.
Save antoine-pous/c73e5a3a3b1e9507597d to your computer and use it in GitHub Desktop.
Check if the localStorage is available
/**
* "THE BEER-WARE LICENSE" (Revision 42):
* <gecko@dvp.io> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return Antoine "Gecko" Pous
*/
LSChecker = function() {
try {
localStorage.setItem('test', 'test');
localStorage.removeItem('test');
return true;
} catch(e) {
return false;
}
}
// Usage :
if(LSChecker()) {
//... do the stuff
}
@ZsharE
Copy link

ZsharE commented Apr 1, 2022

This is much better:

LSChecker = function() {
  try {
    typeof window.localStorage;
    return true;
  } catch(e) {
    return false;
  }
}

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