[whatwg] Declarative unload data
tim at clothears.org.uk
Mon May 7 04:42:00 PDT 2012
On 07 May 2012 at 08:53, Tab Atkins Jr. <jackalmage at gmail.com> 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
More information about the whatwg