spez
spez

Reputation: 469

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:

enter image description here

Upvotes: 1

Views: 997

Answers (1)

Yu Zhou
Yu Zhou

Reputation: 12961

  • 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".

enter image description here

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

Upvotes: 3

Related Questions