Skip to content

Instantly share code, notes, and snippets.

@tkadlec
Last active August 29, 2015 14:17
Show Gist options
  • Save tkadlec/d204a02efe76b0629a75 to your computer and use it in GitHub Desktop.
Save tkadlec/d204a02efe76b0629a75 to your computer and use it in GitHub Desktop.
Cut the mustard with proxy browsers taken into consideration
var hasStorage = (function() {
try {
localStorage.setItem("test", "mod" );
localStorage.removeItem("test");
return true;
} catch (exception) {
// console.info(exception);
if (exception && exception.name=="QuotaExceededError" && localStorage.length == 0) {
//private
return true;
} else {
return false;
}
}
}());
if( 'querySelector' in document && hasStorage && 'addEventListener' in window ){
//cuts the mustard
}
@tkadlec
Copy link
Author

tkadlec commented Mar 28, 2015

Updating the cuts the mustard check to address issues with some proxy browsers (most notably UC Mini) where the original test appears to pass, even though localStorage isn't actually functioning.

The stuff on #8 is to account for private browsing.

@steveworkman
Copy link

I think it can be a little shorter, taking out one of those return true statements if the if statement is inverted. Also, I mentioned http://chrisberkhout.com/blog/localstorage-errors/ for the exception name, but I can't find a list of possible exceptions.

Also, if storage is full (possible, not likely) it would return false as well

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