34

I'm using localStorage in my JS application and I was wondering why IE9 claims localStorage == undefined. As far as I know, IE8 supports it, is here any way to get it working in the new version?

Alex Jasmin
  • 39,094
  • 7
  • 77
  • 67
Mikulas Dite
  • 7,790
  • 9
  • 59
  • 99

3 Answers3

61

Are you testing this on a local HTML file? i.e. a file:/// URL?

localStorage is only available on HTTP websites. That hasn't changed in IE9 Dev Preview.

Alex Jasmin
  • 39,094
  • 7
  • 77
  • 67
  • @AJ. Thanks. I had a suspicion that was the issue, but was still wondering what was wrong when testing in IE. I started running my test page from within an web app and it worked just fine across IE, FF and Chrome. – Stonetip Sep 05 '11 at 03:30
20

IE 11 WORKS

All you need two do add file://127.0.0.1 to the trusted zones under the security tab (NOTE: make sure https check box IS not checked) add this line to the top or your script, depending on your code you may not need to unless you get could not connect to the internet.

!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...");
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
user4822973
  • 201
  • 2
  • 2
  • Great, this works! On my machine even without changing anything in the Security tabs etc. One note: this code throws a `Variable undefined in strict mode` error if you execute your code in strict mode. To solve, re-write in non-condensed mode (`if (!localStorage) { .... }`) – webketje Sep 28 '15 at 01:36
6

Try to open the file like this

file://127.0.0.1/c$/pathtofile/file.html

Gonza
  • 203
  • 2
  • 4