| Latest
Web News |
NSW CIO cuts millions from tech shopping budget After saving millions on hardware and telecoms through shared procurement -- where agencies unite to approach the market as a single buyer -- the New South Wales state government is now considering where else it...
YouTube launches Australian website Video sharing website YouTube has taken on the local accent with the launch of YouTube Australia. YouTube Australia features a localised homepage and search functions, allowing users to create and share videos, discover the most popular and relevant videos...
UN endorses Coonan broadband model Communications Minister Helen Coonan says a UN body has endorsed the government's preferred WiMAX wireless broadband technology. The government and the opposition have been at loggerheads over the...
|
|
10.23.07
An Old School AJAX Call That Just Works
By
Mads Kristensen
Today I was working on a web page that performs an AJAX call on unload, so when a person navigates away from the page, the AJAX call is triggered.
The goal was to register that the visitor navigated away from the page so just a one-way call was needed.
I started out using a simple ASP.NET client callback and it worked - or so I thought.
When the next page on the same website is loaded, a JavaScript error occurred because the __pendingcallback variable was null.
Then I made the callback asynchronous instead of synchronous but it didn't help.
Then a colleague suggested that I should use an ASP.NET AJAX script service call instead. That didn't help.
A JavaScript error still occurred though it was a different one but caused by the same issue.
So I started doing what I should have done from the beginning - do it old school. It's faster, more reliable, uses GET, light weight and completely cross-browser supported.
I created an HttpHandler that took a few URL parameters. I could have used the same page but for this scenario a handler was more appropriate and they are always faster.
Then I added this piece of code in the unload event handler in JavaScript:
The UnLoadHandler() method performs a GET request to the handler by preloading it into an Image object. This is of course not AJAX according to the definition, but it is asynchronous and uses JavaScript. I guess that makes it AJ.
Comments
About
the Author:
Mads Kristensen currently works as a Senior Developer at Traceworks located
in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in
2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and
web services in his daily work as well. A true .NET developer with great passion for the simple solution.
http://www.madskristensen.dk/
|
|