文件协议 (file://) 下的 Javascript/HTML 存储选项

发布于 2024-11-05 16:36:28 字数 410 浏览 4 评论 0原文

我正在开发一个 html 应用程序,它本质上是一系列页面,就像一本书一样。在此应用程序中,我想跨页面存储多个 JavaScript 变量。想想阅读的页面、书签等。当通过 HTTP 查看此应用程序时,我计划使用 localStorage 并为旧版浏览器提供后备(globalStorage、userData 等)。

但是,如果通过“file://”访问文件(例如从 CD 上查看),则这种情况会完全崩溃。似乎大多数(如果不是全部)本地存储解决方案都无法在文件协议下工作。 Cookie 也不是“file://”下的选项。

关于如何在通过“file://”查看 HTML 页面时保留 JavaScript 数据的任何想法吗?

它实际上只需要在当前用户会话中可用。我真的不想使用框架,但到目前为止,我想不出另一种方法来实现这一点

I am developing an html application that is essentially a series of pages like a book. Within this application, I would like to store several JavaScript variables across pages. Think things like pages read, bookmarks, etc. When this application is viewed over HTTP, I plan to use localStorage with fallbacks for older browsers (globalStorage, userData, etc.).

However, this completely breaks down if the files are accessed via "file://", for example if viewed off of a CD. It seems that most (if not all) localStorage solutions will not work under the file protocol. Cookies are not an option either under "file://".

Any ideas on how to persist JavaScript data across html pages when they are being viewed via "file://"?

It really only needs to be available in the current user session. I really don't want to use frames, but as of yet, I can't think of another way to accomplish this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世蒼涼 2024-11-12 16:36:28

我的一个工作伙伴通过分享他使用 window.name 跨单个窗口/会话的页面存储数据的实现帮助我解决了这个问题。

类似的实现(以及围绕该方法的讨论)可以在这里找到:https://web.archive.org/web/20200203061558/http://ajaxian.com/archives/whats-in-a-windowname

我对此方法所做的初步测试看起来非常好有希望的。我在以下浏览器上根据文件协议(从桌面加载页面,又称“文件:\”)对此进行了测试。这对他们所有人都有效!

  • IE 6
  • IE 7
  • IE 8
  • IE 9
  • FF 3.6
  • FF 4
  • Chrome 11
  • Opera 10
  • Safari 4

我还没有对您可以在此处存储多少数据进行任何测试,但互联网似乎同意值为 2 MB。

来源、链接、更多信息

A buddy of mine at work helped me out with this problem by sharing his implementation of using window.name to store data across pages of a single window/session.

A similar implementation (and discussion around the method) can be found here: https://web.archive.org/web/20200203061558/http://ajaxian.com/archives/whats-in-a-windowname

Preliminary tests I've been doing on this method look very promising. I tested this, under the file protocol (loading page from desktop, a.k.a. - "file:\") on the following browsers. It worked on all of them!!

  • IE 6
  • IE 7
  • IE 8
  • IE 9
  • FF 3.6
  • FF 4
  • Chrome 11
  • Opera 10
  • Safari 4

I have not yet done any testing as to how much data you can store here, but the internets seem to agree on a value of 2 MB.

Sources, links, more information

蓝眸 2024-11-12 16:36:28

在 Webkit(Chromium 12.0.742.21 dev-m)上,文件协议上的 localStorage 对我来说效果很好。

在 Gecko 上,已报告了,并且应该很快就会得到改进。

您甚至可以找到解决方法,但我建议您使用 Chrome nightly build 进行开发。因此,您将来不会浪费时间将其移回本地存储。

On Webkit (Chromium 12.0.742.21 dev-m) localStorage over file protocol worked fine to me.

On Gecko it's reported and should be improved soon.

You can even find a workaround, but I recommend that you develop using Chrome nightly build. So you'll not lose time moving it back to localStorage in future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文