简体   繁体   English

使用“localStorage.setItem”Internet Explorer

[英]using "localStorage.setItem" Internet explorer

I want to use localStorage.setItem in internet Explorer 11,我想在 Internet Explorer 11 中使用localStorage.setItem

I tried like:我试过:

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

but in IE console I see:但在 IE 控制台中我看到:

SCRIPT5007: Unable to get property 'setItem' of undefined or null reference. SCRIPT5007:无法获取未定义或空引用的属性“setItem”。

I also tried the given answer like:我也试过给定的答案,如:

<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>

but then I get:但后来我得到:

在此处输入图片说明

  • Please check this option in IE 11, if this option is disabled, storage object will be undefined in IE:请在 IE 11 中选中此选项,如果禁用此选项,则存储对象将在 IE 中未定义:
    1. Click the "Tools" menu and choose "Internet Options".单击“工具”菜单并选择“Internet 选项”。
    2. Click on the tab labeled "Advanced".单击标有“高级”的选项卡。
    3. Check the box for " Enable DOM Storage ".选中“启用 DOM 存储”框。
    4. Click "Apply", click "OK".单击“应用”,单击“确定”。

在此处输入图片说明

  • localstorage can't work with file:// protocal, you need to run the page through http protocal. localstorage不能使用file://协议,你需要通过 http 协议运行页面。
  • There might be issues with localstorage in old version of IE 11 in win7/win8, please make sure you have installed the latest update. win7/win8 老版本IE 11的localstorage可能有问题,请确保你已经安装了最新的更新。

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

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