↑
Main Page
Copycat implementations
Assuming that you have some server-side logic designed to take these headers into account, you can
provide some additional functionality and/or evaluation of requests.
Copycat implementations
This object proved to be so popular among Web developers that other browser makers copied the imple-
mentation. Mozilla was the first of the copycats, creating a JavaScript object called
XMLHttpRequest
that
behaves exactly the same as Microsoft’s version. Both Safari (as of 1.2) and Opera (as of 7.6) copied Mozilla’s
implementation, creating their own
XMLHttpRequest
objects.
To allow creation of an XML HTTP request in a common way, just add this simple wrapper class to your
pages:
if (typeof XMLHttpRequest == “undefined” && window.ActiveXObject) {
function XMLHttpRequest() {
var arrSignatures = [“MSXML2.XMLHTTP.5.0”, “MSXML2.XMLHTTP.4.0”,
“MSXML2.XMLHTTP.3.0”, “MSXML2.XMLHTTP”,
“Microsoft.XMLHTTP”];
for (var i=0; i < arrSignatures.length; i++) {
try {
var oRequest = new ActiveXObject(arrSignatures[i]);
return oRequest;
} catch (oError) {
//ignore
}
}
throw new Error(“MSXML is not installed on your system.”);
}
}
This code allows you to use the following line to create an XML HTTP request in all browsers that support it:
var oRequest = new XMLHttpRequest();
After this point, the XML HTTP request can be used in all supporting browsers as described in the previ-
ous sections.
Performing a GET request
The most common type of request on the Web is a GET request. Every time you enter a URL into your
browser and click Go, you are sending a GET request to a server.
Parameters to a GET request are attached to the end of the URL with a question mark, followed by
name/value pairs separated by an ampersand. For example:
496
Chapter 16
19_579088 ch16.qxd 3/28/05 11:42 AM Page 496
Free JavaScript Editor
Ajax Editor
©
→