AJAX-Driven Web Sites: Under The Hood

Recently, a number of Web sites have begun to raise some eyebrows within the developer community. What's unique about these sites is that they behave more like a desktop application than a Web application. As you interact with them, they quickly display an endless amount of information to your browser without reloading the page.

At the Google Maps site for example (http://maps.google.com/), you can click on the map, zoom in, zoom out, and move around as much as you like. Your browser continues to be fed with data from the server, yet your browser doesn't have to refresh. They're not using applets, or anything like Flash, so how are they doing it?

Introducing Asynchronous JavaScript + XML, also known as Ajax. To properly describe what Ajax is, it's easiest to contrast it with what it's not. For most Web sites, interaction with a Web server is simplex communication - like talking to your buddy on a walkie-talkie. You speak while he receives, and vice versa, but never at the same time. For a Web user, when he or she fills out an online form and then clicks the submit button, the entire page is posted to the Web server and the user must wait for the server to receive the request. When the server finishes processing the request, it sends the processed content back. Only then does the user's page finally refresh (see Figure 1). Ajax is an attempt to alleviate this choppy sequence of events. When the user is at an Ajax Web site the browser can call the Web server asynchronously, behind the scenes - without posting the entire page.

Nuts & Bolts
Currently there's no SDK for Ajax. It's not something you can download. It is actually a conglomeration of several technologies that may or may not even use XML, despite the fact that XML is represented in the Ajax name. Taking a look underneath the hood, we see a mixture of technologies being used. JavaScript, the DOM, XMLHttp, and XML are the key players. Keep in mind though, there are neither standards nor strict definitions for this methodology. What you find in one implementation may differ from another. However, one thing that is common to Ajax implementations is JavaScript.

As the user interacts with the browser, the JavaScript code will handle various events, such as the keystroke or click events, and deal with them accordingly. JavaScript uses the XMLHttpRequest object as the liaison between the browser and the remote server. Microsoft was first to implement the XMLHttpRequest object in Internet Explorer 5 (since then, the other major browsers have added support for this object as well).

The cool thing about the XMLHttp-Request object is that it can talk with the Web server whilst running in the background, asynchronously, without having to reload the page. When the Web server receives a request from a browser, it does its processing and then returns processed XML data back to the browser. The JavaScript engine will receive this processed XML, and use the DOM to manipulate the page elements accordingly. For example, in an Ajax-driven page, such as the Google Suggest site (www.google.com/webhp?complete=1&hl=en), as you type in the search field each letter is sent to the server asynchronously. When typing, words quickly appear below the text. Behind the scenes, it's making several calls to the server for each keystroke. The user isn't hassled by this, because their interaction isn't being interrupted. Only a portion of the page is being refreshed. This can all be done efficiently, because only a fraction of the page data is sent over the wire, rather than the entire page.

An Ajax Example
Let's take a look at a simple Ajax example. It consists of only two pages, HTMLStartPage.htm, and HandleAjaxRequests.aspx. HTMLStartPage.htm contains two text boxes. This demonstrates how characters typed by the user can be sent to the HandleAjaxRequests.aspx page one by one, processed, and then echoed back to HTMLStartPage.htm's second text box. Figure 2 is a visual representation of this example.

The meat of Ajax lies in the JavaScript. Looking at the code, we see that the text box, txtStart, calls the SendValue(val) function for any onKeyUp events within txtStart.

When a user types a character and the SendValue(Val) is called, the HTTPRequest object is initialized first. At this point we determine if the current browser is IE, or some-thing else like Mozilla or Netscape, so we can create the objRequest (HTTPRequest) correctly. Next, to handle things on the server side, we load the "url" variable with the location of the aspx page that will do our processing.

Next, let's look at the three objRequest lines. First, the objRequest.onreadystatechange is called. The onreadystatechange property helps us set up a callback function. This callback will be called only when the readyState property changes; that is, when we get data back from the Web server. The callback function will handle it at that time.

The objRequest.open requires three parameters: a GET or POST, a string for the "url" we defined earlier, and a Boolean value that defines whether this call is asynchronous or not. Note that if this Boolean value is set to true, as it is here, a callback function is required.

The objRequest.send(null) line actually calls the aspx page defined in the "url" variable. However, before we go on to the aspx page, notice the callback function (Process()). Remember, this is the function that will be called (back) after the Web page code has processed our request; it's our reentry point. Here, we simply take the values returned from our aspx page, and put them into the second text box (txtEchoOutPut).

For our aspx page, I made things as simple as possible. It receives the keystroke as a querystring value and I add some text to the string ("You typed:") to prove that we hit the server, and then we echo back the same text. After this aspx page is hit, the callback function (Process()) is fired within the JavaScript, as described earlier.

From the user's perspective, this is all done very quickly. When a user types in the first text box, their letters appear within the second text box immediately. The typed text is actually making a round trip to the server and back.

Ajax Is Not New
It should be noted that Ajax is not new. The methodology has been around for years. Web sites like Google are now proving Ajax's usefulness, stability, and the ability to make the Web closely resemble that of a desktop application: the holy grail of Web development. And what's special about Ajax is that it can do all of this using proven, existing technology. In other words, any standard browser (that can handle JavaScript and the DOM) will work. You don't need to install something separate, like a plug-in.

Here at Magenic, we're taking a look at how this methodology can benefit our clients. Ajax is not something that will replace every Web site, as we know it, but it has a place and it's a skill we want to have in our repertoire.


ASP.Net 2.0
ASP.Net 2.0 significantly enhances the scripting model to incorporate this methodology. They call it script callbacks instead of Ajax. It works essentially the same as I described earlier, but ASP.Net 2.0 takes it a step further by providing tools and support. For a comprehensive explanation of script callbacks in ASP.Net 2.0, take a look at this article: http://msdn.microsoft.com/msdnmag/issues/04/08/CuttingEdge/.

1.  Ajax blurs the line between correct tiering techniques since much of the work has been moved to the client. Consideration should be given when designing such applications using an emerging methodology. The client (browser) is doing more of the processing work, and the JavaScript to accomplish this is fairly complex. It's handling keystrokes, mouse clicks, interaction with the DOM, processing of these events, and data coordination with the server.

2.  It should also be noted that many users might not want to run JavaScript on their browsers. Your Web site audience is a consideration.

3.  The name Ajax is not official. The folks at Adaptive Path are given credit for this catchy name. In ASP.Net 2.0, it is referred to as "script callbacks."


Source code

2005 SYS-CON Media Inc.

Questions or problems regarding this web site should be directed to abeckman@outdoorssite.com.

Copyright 2008 Art Beckman. All rights reserved.

Last Modified: March 9, 2008