简体   繁体   中英

using "localStorage.setItem" Internet explorer

I want to use localStorage.setItem in internet Explorer 11,

I tried like:

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

but in IE console I see:

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

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:
    1. Click the "Tools" menu and choose "Internet Options".
    2. Click on the tab labeled "Advanced".
    3. Check the box for " Enable DOM Storage ".
    4. Click "Apply", click "OK".

在此处输入图片说明

  • localstorage can't work with file:// protocal, you need to run the page through http protocal.
  • There might be issues with localstorage in old version of IE 11 in win7/win8, please make sure you have installed the latest update.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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