21

Both statements window['localStorage'] and window.localStorage

are undefined when accessing the url "file:///C:/index.html"

Is localStorage off limits when running websites on the filesystem?

PS. I'm running the website on a Windows 7 phone hosting the website in isolatedStorage.

PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
DevNull
  • 763
  • 5
  • 17
  • 30

3 Answers3

24

Yeah, IE9 doesn't support localStorage for local files. Not in any official documentation that I can find, but the same issue is described in this blog.

You'll have to either host the website externally, or find some other method of persisting data. [Support for HTML5-style local storage is still in beta in many browsers, anyway. Especially for pages on the local filesystem.]

You could try userdata behaviors, which is a pre-W3C solution developed by Microsoft for Internet Explorer. Not sure if it supports local filesystems, though. Links:

  1. http://www.javascriptkit.com/javatutors/domstorage2.shtml
  2. http://msdn.microsoft.com/en-us/library/ms531424(VS.85).aspx

References:

  1. https://bugzilla.mozilla.org/show_bug.cgi?id=507361
  2. https://stackoverflow.com/a/7377302/1122351
Community
  • 1
  • 1
benesch
  • 5,239
  • 1
  • 22
  • 36
  • Thanks, I'll check out you blog Just found this answer too: http://stackoverflow.com/questions/3392032/using-localstorage-in-ie9-preview – DevNull Jan 02 '12 at 22:39
  • Yeah, stumbled across a similar question too. [See updated links]. Sorry! – benesch Jan 02 '12 at 22:40
  • I believe that WebDB, WebStorage and IndexDB has the same issue Do you now an alternative to localStorage that will work with file://? – DevNull Jan 02 '12 at 22:45
  • userdata behaviors, potentially. I've updated my answer. If that doesn't work, you might be out of luck. Any chance you could just host the site on an actual web server? – benesch Jan 02 '12 at 22:56
  • Perhaps if i could run it on my WP7? Posted this as a new question here: http://stackoverflow.com/questions/8706257/any-ie9-alternative-to-localstorage-that-will-work-with-file-on-the-windows-7 – DevNull Jan 02 '12 at 23:09
5

As an added bonus, IE will swat down any attempt to work around this issue.

The sane thing to do would be to stub out your own dummy localStorage so that at least your thing doesn't break when loading it from the local FileSystem:

if (document.all && !window.localStorage)
{
    window.localStorage = {};
    window.localStorage.removeItem = function () { };
}

Any guesses as to what alert(window.localStorage) will pop up after running that? Did you guess "undefined"???

Thanks, IE! Now there is actually one ugly hack we can do to make this work. Since IE won't let you reuse its reserved word "localStorage", we'll just move the whole thing over to someplace else:

window.localStorageAlias = window.localStorage;
if (document.all && !window.localStorage)
{
    window.localStorageAlias = {};
    window.localStorageAlias.removeItem = function () { };
}

So now, anywhere you'd normally say localStorage['beans'] = 7, you just do localStorageAlias['beans'] = 7 and you're back in business. Naturally, IE won't actually store anything there between sessions when running from the local filesystem. But at least it won't break.

For extra credit, you can fix the above code to swap in some form of persistent storage that IE will actually use when running locally.

Jason Kester
  • 5,951
  • 9
  • 35
  • 40
  • 1
    Interesting hack to avoid crashes. Thanks. But i doubt that there is any persistent storage alternatives to be swapped in. I wish there where. – DevNull Sep 04 '12 at 22:08
1

I have the same problem and found two plugins with fallback solutions:

https://github.com/andris9/jStorage

HTML5 Local Storage fallback solutions

I like the first one more because it is smaller and simpler.

bjb568
  • 11,089
  • 11
  • 50
  • 71
PiTheNumber
  • 22,828
  • 17
  • 107
  • 180
  • does jstorage work when used on a local file, not one on a server? For example, if I have a file at C:\test that uses jstorage, will jstorage work with IE9? See my question at http://stackoverflow.com/q/24210706/2658159. – Aaron Thomas Jun 16 '14 at 14:16