Last active
August 29, 2015 14:17
-
-
Save tkadlec/d204a02efe76b0629a75 to your computer and use it in GitHub Desktop.
Cut the mustard with proxy browsers taken into consideration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
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
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.