views:

41

answers:

1

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

+3  A: 

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.

Nick Craver
Unfortunately, it does not seem to process XHR while in onbeforeunload function/state. Or, at least, it didn't in my case.
Oh, actually, it does work. With non-asynchronous XHR.
As an extra: is there some way to debug stuff that happens on page close?
@hell.orts.ru - You could use Firebug and turn "Persist" mode on so it keeps the data when going to the next page, that's usually the best method for me.
Nick Craver
Ah. It's not actually what I expected, because my subj. page does not have any external links. But I can add some for that specifically, so thanks for the idea.