1

If i were to test for localStorage in the users browser with JavaScript, e.g.

var hasStorage = (function() {
    try {
        localStorage.setItem(mod, mod);
        localStorage.removeItem(mod);
        return true;
    } catch (exception) {
        return false;
    }
}());

if (hasStorage) {
    // store things in places
}

(from: https://mathiasbynens.be/notes/localstorage-pattern)

Can I infer (without explicit checks) that if hasStorage === true, then sessionStorage is also available, or is it possible to have one disabled with the other enabled?

tymothytym
  • 1,571
  • 3
  • 16
  • 25
  • 1
    `localStorage` can be disabled at chromium. There is no option to disable `sessionStorage`. If any, the inference is that if `localStorage` is disabled, `sessionStorage` will also be disabled. This can be verified at chromium by launching with `chromium-browser --disable-local-storage` flag then trying to set `sessionStorage` – guest271314 Apr 17 '17 at 20:48
  • 2
    @blex at the same compatibility table firefox has an opposite support order. While it is possible to assume that in normal operation both are present, there is no specs to warrant such assumption. So, I'd say that to be on the safe side, its good to treat those as separate entities. – Vladimir M Apr 17 '17 at 20:51
  • 1
    Interestingly, launched chromium with `--disable-local-storage` flag set, `sessionStorage` was still defined, trying to set `localStorage` logged `Uncaught TypeError: Cannot read property 'setItem' of null` – guest271314 Apr 17 '17 at 20:54

1 Answers1

3

localStorage does not appear to be related to sessionStorage at chromium. It is possible to launch chromium with --disable-local-storage flag which logs error when trying to set localStorage

Uncaught TypeError: Cannot read property 'setItem' of null

though there does not appear to be an official flag to disable sessionStorage at chromium List of Chromium Command Line Switches. Note, chrome is built using chromium source code.

guest271314
  • 1
  • 15
  • 104
  • 177
  • 1
    That is interesting. I may be misreading, but wouldn't your research also suggest that if `localStorage` **is** available (i.e. not flagged/disabled as you have shown) then `sessionStorage` would also be available as it can't (as far as can be seen from the specs linked) be disabled, so the test would not need re-running for both? – tymothytym Apr 17 '17 at 21:09
  • 1
    `sessionStorage` can be disabled http://stackoverflow.com/questions/17882647/can-user-disable-html5-sessionstorage . Suggest checking each. – guest271314 Apr 17 '17 at 21:10