On 7.5.2012 13:42, Tim Streater wrote:
On 07 May 2012 at 08:53, Tab Atkins Jr.<[email protected]> wrote:
Besides those annoying "Are you sure you want to leave this page?"
dialogs, the primary use of the unload events is a final, desperate
attempt to throw a message at the server, either to save some data or
release some server-held resource. However, this is tricky to do
reliably, because browsers kill pending XHRs as they unload the page.
Right now, I think the most reliable method is to create an<img> and
set its @src, because of an old quirk in IE that other browsers have
copied which means that pending image loads are carried to completion
rather than being killed. This is, obviously, stupid. (Another,
stupider, method is to prevent the handler from exiting by spinning a
no-op loop for several seconds, as I think browsers won't unload the
page until the handler exits?)
Personally I use onbeforeunload. In the handler for that I do a *synchronous*
ajax request to have a tidy-up script run. The tidy-up script spins off a
process that sleeps for 3 seconds before closing down apache. That allows the
tidy-up script time to complete and send the ajax results back, so permitting
the unbeforeunload handler to finish. Since the apache instance is running on
the user's machine, the assumption here is that the three seconds is plenty of
time.
--
Cheers -- Tim
There are any number of other possible tasks that can be performed that
are not synchronous (IDb, workers, etc.), you may want perform the same
task in this event as you do through out the application lifetime and
with this, you would have to have it once async, once sync...
B.