5

Everybody say that window.localStorage is supported by IE (until IE8)

I test it on IE9 but :

console.log(typeof window.localStorage)

undefined

what does it mean?

What's the best way to store local data for all browser?

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
Valeriane
  • 936
  • 3
  • 16
  • 37
  • check compatibility mode – Luca Rainone Nov 09 '12 at 09:19
  • possible duplicate of [local storage in IE9 fails when the website is accessed directly from the file system](http://stackoverflow.com/questions/8706006/local-storage-in-ie9-fails-when-the-website-is-accessed-directly-from-the-file-s) – mercator Nov 09 '12 at 10:08

2 Answers2

7

Local Storage is stored relative to an origin.

This means you must open your page using http://someorigin/pathtoyourpage.html. It can't work on IE if you're opening the page in file://pathtoyourpage.html (and shouldn't work on other browsers).

So you need to access your page using a web server (it can be on localhost).

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    I have no server, I am developping a client side appliation with html5, is not possible to store data in this case? – Valeriane Nov 09 '12 at 09:29
  • You mean you can't install a simple http server ? In this edge case you won't be able to use localStorage in your pages. – Denys Séguret Nov 09 '12 at 09:33
  • I can't install http server, It is not the purpose of my project – Valeriane Nov 09 '12 at 09:36
  • The web has be designed for the network, not for local file access, hence this kind of limits. But if your users can call a website once to fetch your site, your solution might be to use HTML5's [application cache](http://diveintohtml5.info/offline.html). – Denys Séguret Nov 09 '12 at 09:36
  • 3
    @valeri if the purpose of your project is to make a working website, you might want to reconsider that little detail about the server. – mkoryak Nov 09 '12 at 09:41
  • This answer is incorrect. Firefox, Chrome and Opera support localStorage just fine on the local file system. Firefox seems to be a bit more restrictive than the others, only sharing storage between files in the same directory. IE doesn't support it at all, presumably for security reasons. – mercator Nov 09 '12 at 11:44
  • I know they "support" it. Read my answer again. I said they **"shouldn't"** because this is a security breach leading to leaks between local applications. And, probably more importantly, this uselessly clutters the local localstorage. Note that the official w3.org spec regarding the local origin is clearly vague which leads to a few differences between browsers. For most other purposes (for example requests) even Chrome and Firefox consider 2 pages coming from file:// to be of different origins. – Denys Séguret Nov 09 '12 at 11:53
  • 2
    Your answer says nothing about security, but instead seems to say that storage needing an origin precludes local use. [`file:` URLs also have an origin](http://tools.ietf.org/html/rfc6454#section-4). – mercator Nov 09 '12 at 15:39
0

What about Storage.js ?

https://sites.google.com/site/daveschindler/jquery-html5-storage-plugin

"Provides a simple interface for storing data such as user preferences. The storage plugin is useful for saving and retreiving data from the user's browser. For newer browsers, HTML 5's localStorage is used. If localStorage isn't supported, then cookies are used instead. Retrievable data is limited to the same domain. "

Valeriane
  • 936
  • 3
  • 16
  • 37