11

I am creating 2 HTML files that will be stored an an iPhone locally and accessed through a WebView.

I am wondering if it is possible to set localStorage in one file, and get the results of the storage from the other file.

I know that localStorage is accessible from files on the same domain, however, it appears that you can not get the value from a different local file?

I have also tried running this in Safari on the desktop with local files and the same issue occurs.

Jake Chasan
  • 6,290
  • 9
  • 44
  • 90
  • By "local file" do you mean a client-side file? JavaScript does not have client-side file access without requiring the user to save/drag/select the file. – Casey Falk Jul 28 '14 at 14:19
  • Yes, client-side. But I am only attempting to access the localStorage from a file, not the entire file itself. Is the localStorage considered part of the HTML file? – Jake Chasan Jul 28 '14 at 14:20
  • See this question: http://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage . It looks like localStorage is mostly for cookies or key/val pairs -- not for storing full files. That being said, you could store the *content* of your file that way. – Casey Falk Jul 28 '14 at 14:26
  • Ah, I'm slightly wrong. You can use the `FileSystem` API: http://stackoverflow.com/questions/4940586/can-you-use-html5-local-storage-to-store-a-file-if-not-how – Casey Falk Jul 28 '14 at 14:28
  • What about in a cookie? Can one page set a cookie and another page retrieve it? – Jake Chasan Jul 28 '14 at 14:28
  • That's exactly what `localStorage` is for. :) http://stackoverflow.com/questions/5392060/http-cookie-between-two-html-pages – Casey Falk Jul 28 '14 at 14:29
  • Yes, but localStorage does not appear to work for multiple pages hosted client-side. Can a cookie be accessed from multiple pages that are not on the same domain? (because the browsers appear to consider every local file on a separate domain) – Jake Chasan Jul 28 '14 at 14:31
  • 1
    The main issue here is as @TsanyoTsanev pointed it: each file has its own "domain" -- which makes cookies difficult. This SO has some work-arounds: http://stackoverflow.com/questions/3342140/cross-domain-cookies . Long story short: I don't think so. : / – Casey Falk Jul 28 '14 at 14:33

2 Answers2

21

When you are opening the files locally, i.e. using the file:// protocol, as of now the browsers can not determine what is "same domain" so every file is considered a separate domain. Thus you can not use localStorage when you're opening the files.

Here is some more information on the problem in FireFox: https://bugzilla.mozilla.org/show_bug.cgi?id=507361 . Personally I couldn't find much about Safari on this topic.

You can also look over this: Javascript/HTML Storage Options Under File Protocol (file://) . It might be helpful in your situation.

Community
  • 1
  • 1
Tsanyo Tsanev
  • 1,269
  • 10
  • 11
  • This doesn't appear true on iOS 10 with local files rendered via `WKWebView` – Crashalot Nov 01 '16 at 23:27
  • 1
    This doesn't seem to be true on Chrome any more. I can set local storage variables on `file://` page with no issue, and read them again after closing and re-opening the browser – Kip Aug 06 '20 at 18:32
2

At this time, local files rendered via WKWebView on iOS 10 share access to the "same domain" with respect to localStorage. Unclear whether this is by design or a bug, however.

Crashalot
  • 33,605
  • 61
  • 269
  • 439