Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localStorage object is undefined in IE

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?

like image 702
Mikulas Dite Avatar asked Aug 02 '10 21:08

Mikulas Dite


People also ask

Does localStorage work in IE?

The localStorage object stores the data like a persistent cookie, with no expiration date. The data will not be deleted when the browser is closed, and will be available when a user returns to the browser. IE also supports localStorage from IE8 but it does not support localStorage in IE7 and previous versions.

Why is local storage undefined?

The localStorage property is a property on the window object, therefore it's not available on the server. Copied! If you got the error in the browser, make sure that you have not misspelled the localStorage keyword (should be camelCase).


2 Answers

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.

like image 125
Alex Jasmin Avatar answered Sep 28 '22 01:09

Alex Jasmin


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..."); } 
like image 41
user4822973 Avatar answered Sep 28 '22 02:09

user4822973