通知服务器(XMLHttpRequest?) onunload 事件?
当客户端离开页面时,是否可以通知服务器(发出带有一点数据的单个 HTTP 请求,响应并不重要)?
我实际上正在使用带有 javascript 的 python-tornado comet 应用程序,不断保持与服务器的请求连接(服务器关闭并在事件发生时重新打开。基于此: https://launchpad.net/eftw)。
我没有找到一种简单的方法来调试它(特别是使用 FireBug)。这使得它很有问题。
哦,还有实际的代码(因为它已经有点可用了): http:// bazaar.launchpad.net/~hoverhell/xftw/trunk/files
Is it possible to notify a server (make a single HTTP request with a bit of data, response is unimportant) when client leaves the page?
I'm actually using python-tornado comet application with javascript constantly keeping a request connection to the server (which gets closed and re-opened on event. Based off this: https://launchpad.net/eftw).
I didn't find a easy way to debug this (with FireBug, in particular). That makes it quite problematic.
Oh, and the actual code (since it somewhat usable already anyway): http://bazaar.launchpad.net/~hoverhell/xftw/trunk/files
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法以可靠的方式执行此操作,具体取决于浏览器和延迟,您的 XmlHttpRequest 可能会在实际完成之前被终止。
浏览器制造商希望(在我看来是正确的)尽快渲染下一页。
onbeforeunload
和onunload
是清理事件,旨在快速处理剩余的任何变量,它们不应该被等待......所以浏览器不会。由于较新浏览器中的垃圾收集也得到了显着改进,因此浏览器甚至没有理由等待这些事件。您可以在
window.onbeforeunload
事件中发送请求...但是它会到达服务器吗?或许。You can't do this in a reliable way, depending on browser and latency, your XmlHttpRequest is likely to be killed before it actually finishes.
Browser makers want to (correctly, imo) render the next page as quickly as possible. The
onbeforeunload
andonunload
are cleanup events meant to quickly dispose of any variables left around, they're not meant to be waited on...so the browser doesn't. Since garbage collection in newer browsers has also improved significantly, the browser has even less reason to wait on these events.You can send a request in the
window.onbeforeunload
event...but will it get to the server? Maybe.