使用哪个事件处理程序来记录离开页面 - onunload 或 onbeforeunload?

发布于 2024-09-01 11:26:29 字数 324 浏览 3 评论 0原文

由于我之前关于使用 javascript 测量页面转动时间的问题没有任何答案,我将开始编写自己的代码(!)。

为了测量领带所需的长度,我建议在用户浏览离开页面时删除包含时间戳的 cookie,然后在后续页面中将该时间与“现在”进行比较,并将请求发送回以下 URL:将记录间隔。

似乎有 2 个可能的处理程序可以将第一个代码块与 onunload() 处理程序或 onbeforeunload() 相关联?

更重要的是,它在不支持或损坏处理程序的浏览器上默默地失败,而不是在每个可能的浏览器上移植。

有什么指点吗?有什么问题吗?

蒂亚

C.

Having not any answers to my previous questions about using javascript to measure page turn times, I'm going to start writing my own code (!).

To measure the length of tie it takes, I'm proposing dropping a cookie containing a timestamp when the user browses away from a page, then in a subsequent page, comparing that time with 'now' and sending back a request to a URL which will log the interval.

It seems that there are 2 possible handlers I could associate the first block of code with - the onunload() handler or the onbeforeunload()?

It's more important that it fail silently on browsers with no/broken support for the handler, than it be portable across every possible browser.

Any pointers? Gotchas?

TIA

C.

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

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

发布评论

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

评论(3

掩耳倾听 2024-09-08 11:26:29

Opera 不支持 onbeforeunload,它的真正目的是让用户有机会中止卸载,因此您应该使用 onunload

onbeforeunload is not supported by Opera, and is really meant to be used to give the user a chance to abort the unload, so you should go for onunload.

奶茶白久 2024-09-08 11:26:29

onunload 或 onbeforeunload 的一件事是任何 Ajax 调用都必须具有 async: false 属性。这可以防止代码在调用完成之前实际离开。除此之外,请参阅 Sean Kinsey 的回答。

One thing with either onunload or onbeforeunload is that any Ajax call must have async: false property. This prevents the code from actually leaving until the call completes. Other than that, see Sean Kinsey's answer.

岁吢 2024-09-08 11:26:29

我正在尝试在 Firefox 中使用 onunload ,但似乎它没有给 Javascript 太多时间来做任何有用的事情,所以这是一个跨浏览器问题。
我可能想结合使用两个处理程序。

I'm trying to use onunload in Firefox but it seems like it doesn't give the Javascript much time to do anything useful, so it's a cross browser issue.
I might want to use a combination of both handlers.

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