开发者

notifying the server (XMLHttpRequest?) on the onunload event?

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 开发者_如何学JAVAeasy 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


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 and onunload 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜