Datasheet

Chapter 1: AJAX Technologies
4
This chapter provides an overview of the following client-side technologies that form the foundations of
the above three main AJAX engine components in the context of an example:
XMLHttpRequest
DHTML
XML
JSON
XML HttpRequest
XMLHttpRequest is one of the main AJAX technologies that the scheduler component of an AJAX
engine uses to make asynchronous requests to the server. The instantiation process of the
XMLHttpRequest object is browser-dependent. Listing 1-1 encapsulates the browser-dependent nature
of this instantiation process in a class named
XMLHttpRequest .
Listing 1-1: Instantiating XMLHttpRequest
if (!window.XMLHttpRequest)
{
window.XMLHttpRequest = function window$XMLHttpRequest()
{
var progIDs = [ ‘Msxml2.XMLHTTP’, ‘Microsoft.XMLHTTP’ ];
for (var i = 0; i < progIDs.length; i++)
{
try
{
var xmlHttp = new ActiveXObject(progIDs[i]);
return xmlHttp;
}
catch (ex) {}
}
return null;
}
}
This script first checks whether the window object already contains a definition for this class. If not, it
defines the constructor of the class. The constructor contains the following array of program ids:
var progIDs = [ ‘Msxml2.XMLHTTP’, ‘Microsoft.XMLHTTP’ ];
This array covers all the possible instantiation scenarios on Internet Explorer. The constructor iterates
through the program ids array and takes the following steps for each enumerated program id:
1. It instantiates an ActiveXObject , passing in the enumerated program id.
2. If the instantiation succeeds, it returns this ActiveXObject instance.
3. If the instantiation fails, the try block throws an exception, which the catch block catches and
forces the loop to move to the next iteration, where the next program id is used.
c01.indd 4c01.indd 4 8/20/07 5:40:03 PM8/20/07 5:40:03 PM