Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

localStorage access from local file

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.

like image 978
Jake Chasan Avatar asked Jul 28 '14 14:07

Jake Chasan


People also ask

How to store items in local storage using localStorage?

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:

What is localStorage in window window?

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

What does the read-only localStorage property do?

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.

How to retrieve the value of the second item in localStorage?

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.


2 Answers

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.

like image 183
Tsanyo Tsanev Avatar answered Sep 21 '22 14:09

Tsanyo Tsanev


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.

like image 32
Crashalot Avatar answered Sep 23 '22 14:09

Crashalot