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;
}
The first lines inside the
try
block might already fail with an exception:Just read back in this gist. FF will in some cases throw an exception if you touch window.localStorage.... IE might give you a null object... So I don't see how the extra checks for the exception type help. In fact I think they actively break the code.
The way I approach it, if simply setting and removing an item from storage fails in any way then storage is not available.
I stand by my original version. I have given up on maintaining the MDN page. People keep coming by and 'fixing' the code sample. I have even seen people change it to samples that don't even run at all or always return true etc. My original sample works fine and does not need those extra lines imho. In fact the code becomes less reliable because of those extra lines.