使用 "localStorage.setItem" Internet Explorer

using "localStorage.setItem" Internet explorer

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

我试过:

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

但在 IE 控制台中我看到:

SCRIPT5007: Unable to get property 'setItem' of undefined or null reference.

我也试过 given answer 像:

<script>
!localStorage && (l = location, p = l.pathname.replace(/(^..)(:)/, "$$"), (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>

但后来我得到:

  • 请在IE 11中勾选此选项,如果禁用此选项,存储对象将在IE中未定义:
    1. 单击 "Tools" 菜单并选择 "Internet Options"。
    2. 单击标有 "Advanced" 的选项卡。
    3. 选中“启用DOM存储”复选框。
    4. 点击"Apply",点击"OK"。

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