Last active
August 29, 2015 14:06
-
-
Save taneltm/e26db536ce6724bf0fe8 to your computer and use it in GitHub Desktop.
Check for localStorage
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
/** | |
* This tests if the localStorage is available or not. | |
* Alternatively, you can check how modernizr does it: | |
* https://github.com/Modernizr/Modernizr/blob/master/feature-detects/storage/localstorage.js | |
* | |
* If you check just (typeof window.Storage != "undefined"), | |
* it will not detect if the user is allowing localStorage or not. | |
* | |
* If you check (typeof window.localStorage != "undefined"), | |
* it will throw a SecurityException if the user has disallowed localStorage. | |
*/ | |
var hasStorage = (function() { | |
try { | |
var ls = window.localStorage, | |
t1 = "--storage-test--", | |
t2 = null; | |
ls.setItem(t1, t1); | |
t2 = ls.getItem(t1); | |
ls.removeItem(t1); | |
return (t1 === t2); | |
} catch (exception) { | |
return false; | |
} | |
}()); | |
if (hasStorage) { | |
// Use the localStorage | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment