A timeline of the last four years of detecting good old window.localStorage
.
October 2009: 5059daa
(typeof window.localStorage != 'undefined')
November 2009: 15020e7
!!window.localStorage
December 2009: 1e0ba91
!!('localStorage' in window)
January 2010: d8947c9
(localStorage in window) && window[localStorage] !== null
July 2010: ef2c47
try {
return ('localStorage' in window) && window[localstorage] !== null;
} catch(e) {
return false;
}
December 2010: c630c39
try {
return !!localStorage.getItem;
} catch(e) {
return false;
}
October 2011: 5e2fa0e
try {
return !!localStorage.getItem('getItem');
} catch(e) {
return false;
}
October 2011: a93625c
try {
localStorage.setItem(mod, mod);
localStorage.removeItem(mod);
return true;
} catch(e) {
return false;
}
by way of interest, there's a slight difference in the current version of the code put in the comment above from mpavel (commented on Apr 18, 2016) regarding the Web Storage API 'Basic Concepts' page section entitled:
"Here is a function that detects whether localStorage is both supported and available:"
mpavel in April 2016 put this:
catch(e) { return false; }
today in December 2017 this snippet reads thus:
catch(e) { return e instanceof DOMException && ( // everything except Firefox e.code === 22 || // Firefox e.code === 1014 || // test name field too, because code might not be present // everything except Firefox e.name === 'QuotaExceededError' || // Firefox e.name === 'NS_ERROR_DOM_QUOTA_REACHED') && // acknowledge QuotaExceededError only if there's something already stored storage.length !== 0; }