【问题标题】:using "localStorage.setItem" Internet explorer使用“localStorage.setItem”Internet Explorer
【发布时间】:2023-03-24 15:59:01
【问题描述】:

我想在 Internet Explorer 11 中使用 localStorage.setItem

我试过了:

<script>
  localStorage.setItem("lastname", "Smith");
</script>

但在 IE 控制台中我看到:

SCRIPT5007:无法获取未定义或空引用的属性“setItem”。

我也试过given answer 喜欢:

<script>
!localStorage && (l = location, p = l.pathname.replace(/(^..)(:)/, "$1$$"), (l.href = l.protocol + "//127.0.0.1" + p));

if (typeof(Storage) != "undefined") {
    // Store
    localStorage.setItem("lastname", "Smith");
    // Retrieve
    alert(localStorage.getItem("lastname"));
} else {
    alert("Sorry, your browser does not support Web Storage...");
}
</script>

然后我得到:

【问题讨论】:

  • 错误页面不太可能是JS错误
  • 您的链接不适合测试 localStorage
  • @mplungjan 很棒的链接,但仍然不起作用,我尝试了window.webstorage.setItem('key', 'value'); 然后我得到了Unable to get property 'setItem' of undefined or null reference

标签: javascript local-storage internet-explorer-11


【解决方案1】:
  • 请在 IE 11 中勾选此选项,如果禁用此选项,存储对象将在 IE 中未定义:
    1. 单击“工具”菜单并选择“Internet 选项”。
    2. 点击标有“高级”的标签。
    3. 选中“启用 DOM 存储”复选框。
    4. 点击“应用”,点击“确定”。

  • localstorage 不能使用file:// 协议,需要通过http协议运行页面。
  • win7/win8旧版IE 11可能存在issueslocalstorage,请确保您已安装最新更新。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多