在窗口卸载时使用 HTML5 Web SQL 保存状态

发布于 2024-09-10 06:16:53 字数 569 浏览 7 评论 0原文

我使用 Web SQL API 在页面上存储一些信息;我想在用户关闭页面或导航离开时保存页面的状态,因此,我从以下开始:

window.onunload = function () {
  db.transaction(function (tx) {
    executeSql("INSERT INTO State (foo) VALUES (?)", [foo]);
  });
};

但是,这是异步的,因此在页面消失之前它不会完成。

我通过在我的 window.onunload 末尾添加一个(不诚实的,因为它还没有发生)alert("Saved!"); 来解决这个问题,效果并不理想。延迟卸载,直到数据库有机会执行其操作,但如果可以避免的话,我宁愿没有该警报。

有什么想法吗?我需要暂时阻止调用 onunload 函数的线程,这就是警报的作用。

(顺便说一句,为了阻止任何关于我使用同步 openDatabaseSync 版本的建议,该 API 仅为 Web Workers 而不是为 Window 对象指定和实现。)

I'm using the Web SQL API to store some information on a page; I'd like to save the state of the page when the user closes the page or navigates away, thus, I start with:

window.onunload = function () {
  db.transaction(function (tx) {
    executeSql("INSERT INTO State (foo) VALUES (?)", [foo]);
  });
};

However, this is asynchronous so it doesn't complete before the page goes away.

I solve this unsatisfactorily by adding a (disingenuous, since it hasn't happened yet) alert("Saved!"); at the end of my window.onunload, which delays the unload until the DB has a chance to do its thing, but I'd rather not have that alert if I can avoid it.

Any ideas? I need to sort of block the thread calling the onunload function for a moment, which is what the alert does.

(BTW, to head off any suggestion that I use the synchronous openDatabaseSync version, that API is speced and implemented only for Web Workers, not for the Window object.)

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

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

发布评论

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

评论(3

笑忘罢 2024-09-17 06:16:53

您可以访问 WebSocket 吗?

您可以侦听一个套接字,然后在卸载时同步连接到该侦听套接字吗?当侦听器收到连接时,它会启动 insert 并在查询完成后关闭连接。

这是我唯一能想到的,我不确定这些接口是否支持这种用法。我通过思考如果我有服务器我将如何做到这一点来解决这个问题:我将使用与服务器的同步网络连接来写入状态。

另外,我不确定卸载事件是否可以被阻止一段不确定的时间,但一些谷歌搜索“卸载同步ajax”表明它可能是这样。

更新 7/19 10:26pm PDT

浏览器 WebSocket 是仅发送的,您无法打开套接字进行侦听。

Do you have access to WebSockets?

Could you listen on a socket, and then when it's time to unload you connect to that listening socket, synchronously. When the listener receives a connection, it starts the insert and closes the connection after the query is done.

This is the only thing I could think of, and I am not sure that these interfaces support this kind of usage. I approached this by thinking how I would do this if I had a server: I would use a synchronous network connection to the server to write the state.

Also, I am not sure that the unload event can be blocked for an indeterminate length of time, but some Googling for "unload synchronous ajax" suggests it could be.

Update 7/19 10:26pm PDT

Browser WebSockets are send-only, you can't open a socket for listening.

吻风 2024-09-17 06:16:53

我只想为后代提到我遇到的另一个想法是打开一个带有超时的模式对话框,如下所示:

window.showModalDialog('javascript:document.writeln ("<script>window.setTimeout(function () { window.close(); }, 100);</script>")');

但这也许并不完全是跨平台的。并且对话框窗口在某些浏览器(至少 Safari)中可见。

I'll just mention for posterity that another idea I've come across is to open a modal dialog with a timeout, like this:

window.showModalDialog('javascript:document.writeln ("<script>window.setTimeout(function () { window.close(); }, 100);</script>")');

But that's not completely cross-platform, perhaps. And the dialog window is visible in some browsers (Safari at least).

作业与我同在 2024-09-17 06:16:53

据报道,有一种方法可以使 SQL 调用同步。

同步Web SQL 是Web SQL 规范草案的一部分。
然而,这部分还没有在任何浏览器中实现。
而且规范草案不会去任何地方,因为只有一个数据库 - SQLite。

然而,appcelerator 中的公平人士以某种方式做到了这一点< /a>.如果我知道他们是怎么做到的就好了。如果您能弄清楚,请更新此线程。

There is reportedly a way of making SQL calls synchronous.

Synchronous Web SQL is part of the Web SQL specification draft.
However this part is the one that was not implemented in any browser yet.
And the specification draft is not going anywhere, because there is just one DB - SQLite.

However the fair folks in appcelerator somehow did the trick. Damn if I know how they did it though. If you can figure it out, please update this thread.

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