0

I was using the following code:

//if (Modernizr.localstorage) {
//    // window.localStorage is available!
//} else {
//    // document.location.href = '/Error/Message/OldBrowser';
//}

Is there some way that I can do the same thing without the overhead of using Modernizr?

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • 1
    Try here: http://stackoverflow.com/questions/4706347/javascript-comparing-2-checks-for-localstorage – Xyan Ewing Aug 02 '12 at 12:41
  • 2
    Exactly how much overhead is Modernizr if you only include that one test? Anyway, you can just copy the test in the Modernizr source: https://github.com/Modernizr/Modernizr/blob/master/modernizr.js#L779 – millimoose Aug 02 '12 at 12:42
  • I would rely on modernizr as it will deal with browser evolution. And it is so small in size that you may afford it. – unludo Aug 02 '12 at 12:48
  • possible duplicate of [How to detect if browser supports HTML5 Local Storage](http://stackoverflow.com/questions/11214404/how-to-detect-if-browser-supports-html5-local-storage) – Felix Kling Aug 02 '12 at 12:54

2 Answers2

2
if (!!window.localStorage) {
   /* you have localstorage */
}

note: localstorage is natively available even on IE8

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
1

From DevProConnections magazine:

function supportStorage()
{
    return ('localStorage' in window) && window['localStorage'] != null;
}
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69