-3

W3Schools recommends (http://www.w3schools.com/html/html5_webstorage.asp)

if(typeof(Storage) !== "undefined") {
    // Code for localStorage/sessionStorage.
} else {
    // Sorry! No Web Storage support..
}

to check whether a local storage is available in the browser.

I use the following

var l=localStorage!==null?localStorage:0;
if(l) { /*code for locale storage*/}

and was wondering whether this is sufficient and reliable on all browsers?

Thx I really appreciate your help!

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
Manuel
  • 9,112
  • 13
  • 70
  • 110
  • @gilly3 thx, searching for the problem I somehow missed this question. However, I think your answer is better than the once from the old thread. – Manuel Aug 04 '14 at 07:13

3 Answers3

13
  1. w3fools – You seem to think that w3schools is associated with the w3c – they're not.

  2. Use window.localStorage. Without window, you'll get a ReferenceError in browsers that don't support localStorage.

  3. Don't use localStorage !== null. In non supporting browsers, localStorage will be undefined, not null. The better operator would be != null, but I wouldn't use a comparison operator at all. You can just pass window.localStorage straight to the ternary operator (l = window.localStorage ? localStorage : 0), or better yet, use ||:

var l = window.localStorage || 0;

Edit: Really, this is all you need:

if (window.localStorage) {
    // do stuff with localStorage
    // no need to use window anymore
}
gilly3
  • 87,962
  • 25
  • 144
  • 176
  • so I don't need the try and catch part? – Manuel Aug 04 '14 at 06:04
  • Usually the try catch is more than you need. It handles the case where some script has created a global variable called `localStorage`. As long as you are aware of what scripts are included in your page, you don't need the try catch. If, on the other hand, you are writing a library or plugin that will be included in other sites that are out of your control, then using the try catch technique is more reasonable. – gilly3 Aug 04 '14 at 15:31
  • Are `'localeStorage' in window` or `window.hasOwnProperty('localStorage')` also a good ways to check for availability? – zanona Mar 23 '20 at 09:16
  • @zanona - those would probably work fine for most cases. They have some downsides that would only matter in obscure cases. Personally, I find their extra verbosity unappealing. – gilly3 Mar 27 '20 at 00:18
1

You could use Modernizr to check this.

Just like in the answer here: How to detect if browser supports HTML5 Local Storage

Jerodev
  • 32,252
  • 11
  • 87
  • 108
0

Sometimes browsers block the access to the localStorage, like when you are browsing in incognito mode or private mode and so on... So checking if the storage exists is not the best idea, because sometimes it exists, but is not accessible.

You can use this function:

function storageON() {
    try {
        localStorage.setItem("__test", "data");
    } catch (e) {
        return false;
    } 
    return true;
}

if (storageON()) { /* DO SOMETHING */ }
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • OK thx, I'm setting var defaults before the localstorage code and just gonna wrap my code using local storage in try and catch. Thx for the hint. – Manuel Aug 03 '14 at 20:42
  • 1
    @MacroBonelli - Looks like Modernizr is also using the same method to check if `localStorage` exists. (You might want to add `.removeItem` to remove `__test` though) – Derek 朕會功夫 Aug 03 '14 at 20:47
  • 1
    This is not great because it does the try-catch every time `storageON` is called. – lonesomeday Aug 03 '14 at 20:52
  • Yeah, and there's absolutely nothing wrong with it. Try catch is a damn statement, not the devil. – Marco Bonelli Feb 19 '17 at 00:23