Making AJAX calls onUnload
I suppose this will be my first “official” developer blog entry, so bear with me if it seems a little sparse. This summer I’ve been doing a ton of work with object oriented Javascript and AJAX, and up until today the AJAX part has been mostly simplified by Atlas webservice proxies. However, today I hit one of those problems that just hasn’t been very well documented by anybody else, so I thought I’d write something about it.
Basically, our application keeps a form of session state server-side, and it needs to be notified when the browser window is closed or refreshed. Clearly, the thing to do here is to hook into the window.onUnload event (side note for new Atlas developers: don’t use window.onUnload, use Sys.Application.unload.add([yourUnloadHandler]) instead).
So at first I just went happily along, sending the usual XmlHttpRequest when the page unloaded. However, this of course poses a problem with the asynchronous response from the server: by the time the response comes back, the page has unloaded and the callback function won’t exist anymore. This generates an ugly Javacript “function is not defined” error. In any case, here’s a nice little picture of what happens:

The solution here is to make sure that any AJAX requests that you make onUnload are make synchronously instead of asynchronously (SJAX?). This will ensure that the page doesn’t finish unloading before the server response comes back. Hence, the much happier picture below:

Bertrand Le Roy has a quick chunk of Javascript that you can use to do alter the XmlHttpRequest object to temporarily do only synchronous calls. Use this Javascript before any AJAX calls in your unload event, and you’ve solved your problem!
May 15th, 2007 at 11:17 am
Hi there,
I’m coding an ASP.net website and the synchronous solution doesn’t solve my problem : the webservice method that I call isn’t triggered everytime… (IE / FF)
It’s very strange because it perfectly works when I display an alert box at the end of the handler
Any idea ?
June 4th, 2007 at 9:12 pm
Thanks for the great tutorial. When using it locally, how can I capture the fact that the user is logged in. I added your sample code to a controller and the filter chain continuously kicks me out. Thanks for the help.
June 5th, 2007 at 5:33 am
Good article.
Asynchronous calls reach server for IE (I have no need for retrieving response) but have problems with Firefox(Suppose problems may exist with other browsers too). So using Synchronous calls in onUnload event solved problem.
Easy way for making Sync calls is to pass false as the third parameter in the AJAX.open method and no need to set up an onreadystatechange handler.
June 13th, 2007 at 7:11 am
I have exactly the same problem as Florian - web service does not get called unless I put an ‘alert’ at the end of the onunload handler. Anyone got any leads here?
June 14th, 2007 at 11:24 am
Hello! Good Site! Thanks you! ddgcpwkotex
August 16th, 2007 at 10:45 pm
Very good site. Thanks.
http://phenterminecheap.jed.pl cheap phentermine