javascript 从本地文件访问 localStorage

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24997292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:43:16  来源:igfitidea点击:

localStorage access from local file

javascripthtmllocal-storage

提问by Jake Chasan

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

我正在创建 2 个HTML文件,这些文件将存储在 iPhone 本地并通过WebView.

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

我想知道是否可以localStorage在一个文件中设置,并从另一个文件中获取存储结果。

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

我知道localStorage可以从同一域中的文件访问,但是,您似乎无法从不同的本地文件中获取该值?

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

我还尝试在桌面上的 Safari 中使用本地文件运行它,但出现了同样的问题。

回答by Tsanyo Tsanev

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 localStoragewhen you're opening the files.

当您在本地打开文件时,即使用file://协议,目前浏览器无法确定什么是“相同域”,因此每个文件都被视为一个单独的域。因此localStorage,您在打开文件时无法使用。

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.

以下是有关 FireFox 问题的更多信息:https://bugzilla.mozilla.org/show_bug.cgi ?id =507361。我个人在这个主题上找不到太多关于 Safari 的信息。

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

您还可以查看以下内容:文件协议下的 Javascript/HTML 存储选项 (file://)。它可能对您的情况有所帮助。

回答by Crashalot

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.

此时,在 iOS 10 上通过 WKWebView 呈现的本地文件共享对“相同域”的访问localStorage。但是,不清楚这是设计使然还是错误。