localStorage object is undefined in IE
Asked Answered
Z

3

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?

Ziwot answered 2/8, 2010 at 21:43 Comment(4)
Are you testing this on a local HTML file with file file:// protocol?Bremser
Typing localStorage in the Dev Tools console of the Platform Preview works for me on http sitesBremser
@Akexandre Jasmin oh you are right, I've forgot it does not work. Thank you.Ziwot
Okay. I wrote an answer explaining the problem. You may want to accept it.Bremser
B
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.

Bremser answered 2/8, 2010 at 22:29 Comment(1)
@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.Ryle
C
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...");
}
Cabernet answered 23/4, 2015 at 7:34 Comment(1)
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) { .... })Byline
H
6

Try to open the file like this

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

Hairtail answered 30/8, 2013 at 19:41 Comment(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.Formative
@daylight It works if you also add file://127.0.0.1 to the list of trusted sites.Tetra
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.Fibrosis
Worked for me on IE11Landwehr

© 2022 - 2024 — McMap. All rights reserved.