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.
This function is used to store items in local storage. An example of this function is shown below. window.localStorage.setItem("grade","One"); //in this case, the `grade` is the key while `One` is the value. As mentioned before, we must stringify objects before we store them in the local storage. An example is outlined below:
Window.localStorage 1 Syntax. A Storage object which can be used to access the current origin's local storage space. ... 2 Description. The keys and the values stored with localStorage are always in the UTF-16 DOMString format, which uses two bytes per character. 3 Example. ... 4 Specifications. ... 5 Browser compatibility 6 See also
The read-only localStorage property allows you to access a Storage object for the Document's origin; the stored data is saved across browser sessions.
var answer = localStorage.key(1); // this statement will retrieve the value of the second item in localStorage. The key () can also be used in a loop statement to retrieve all the items in the local storage. This function is used to store items in local storage.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With