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?
Asked
Active
Viewed 3.4k times
34

Alex Jasmin
- 39,094
- 7
- 77
- 67

Mikulas Dite
- 7,790
- 9
- 59
- 99
-
10Are you testing this on a local HTML file with file `file://` protocol? – Alex Jasmin Aug 02 '10 at 21:52
-
Typing `localStorage` in the Dev Tools console of the Platform Preview works for me on http sites – Alex Jasmin Aug 02 '10 at 22:03
-
@Akexandre Jasmin oh you are right, I've forgot it does not work. Thank you. – Mikulas Dite Aug 02 '10 at 22:20
-
Okay. I wrote an answer explaining the problem. You may want to accept it. – Alex Jasmin Aug 02 '10 at 22:30
3 Answers
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
-
I don't think that works. Couldn't get it to work in IE11, at least. Ah, well, use mongoose web server, it is small enough and then localSTorage works. – raddevus Dec 17 '13 at 21:23
-
3@daylight It works if you also add `file://127.0.0.1` to the list of trusted sites. – grammar31 Mar 17 '16 at 17:07
-
On IE11, this does not seem to work. I can't access a local file using `file://127.0.0.1/...`. I've add `file://127.0.0.1` to the IE Trusted Sites list. – Daryl McCullough Apr 18 '18 at 19:12
-