Most elegant way to check sessionStorage support?
function checkSessionStorage()
{
return window.sessionStorage;
}
If it is undefined
then sessionStorage
is not supported.
I would use try/catch to check if the browser supports sessionStorage.
function isSessionStorageSupported() {
try {
var storage = window.sessionStorage;
storage.setItem('test', 'test');
storage.removeItem('test');
return true;
} catch (e) {
return false;
}
}
Use the function like this:
if (isSessionStorageSupported()) {
// do something with it
} else {
// have a fallback code here
}
if (cookie1 === '9oz' || (window.sessionStorage && window.sessionStorage.getItem('sessionstoragecookie1') === '9oz')) {
// you've got a 9oz reference
} else {
// you haven't :(
}
if(typeof(sessionStorage) == 'undefined')
{
sessionStorage = {
getItem: function(){},
setItem: function(){},
clear: function(){},
removeItem: function(){}
};
}
And now use as usual. It will always return NULL
But I would consider this script
http://code.google.com/p/sessionstorage/
This will enable sessionStorage in every browser.