简体   繁体   English

我可以通过测试localStorage推断sessionStorage的可用性吗?

[英]Can I infer the availability of sessionStorage from testing localStorage?

If i were to test for localStorage in the users browser with JavaScript, eg 如果我要使用JavaScript在用户浏览器中测试localStorage ,例如

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 ) (摘自: 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? 我可以推断出(如果没有显式检查的话),如果hasStorage === true ,则sessionStorage也可用,或者是否可以禁用一个而启用另一个?

localStorage does not appear to be related to sessionStorage at chromium. localStorage似乎与sessionStorage无关。 It is possible to launch chromium with --disable-local-storage flag which logs error when trying to set localStorage 可以使用--disable-local-storage标志启动--disable-local-storage ,在尝试设置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 . 尽管在Chromium命令行开关的表中似乎没有官方标志来禁用sessionStorage Note, chrome is built using chromium source code. 注意,铬是使用铬源代码构建的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM