문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top