简体   繁体   English

检测浏览器是否允许设置localStorage.setItem();

[英]detect if browser allows setting localStorage.setItem();

When using IE11 and using javascript's localStorage features, the browser console blocks javascript from being executed by throwing a message 'access denied' because of security settings within the browser. 当使用IE11并使用javascript的localStorage功能时,由于浏览器内的安全设置,浏览器控制台会通过抛出“拒绝访问”消息来阻止javascript执行。

Is there a way to detect via javascript of these settings are enabled? 有没有办法通过javascript检测这些设置是否已启用?

I tried: 我试过了:

if(localStorage.setItem('testvar','123')){ alert('ok');}else{ alert('not ok');} if(localStorage.setItem('testvar','123')){alert('ok');} else {alert('not ok');}

but still caused the rest of the script to halt. 但仍然导致脚本的其余部分暂停。

thank you 谢谢

function supports_html5_storage() {
  try {
    if ('localStorage' in window && window['localStorage'] !== null)
        {
          localStorage.setItem("testitem",true);
          localStorage.removeItem("testitem");
          return true;
        }
  } catch (e) {
    return false;
  }

}

Source: diveintohtml5 资料来源: diveintohtml5

EDIT: Added localStorage.setItem to check, not only if it exists but if it is writable. 编辑:添加了localStorage.setItem进行检查,不仅它是否存在而且是否可写。

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

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