Detect Browser Compatibility with the Request Object - Ajax Basics
(Page 3 of 4 )
In this case, thewindow.XMLHttpRequestobject will not exist in the browser object model. Therefore, another branch of theiftest is necessary in your code:
else if (window.ActiveXObject){
request=new ActiveXObject("Microsoft.XMLHTTP");
if (! request){
request=new ActiveXObject("Msxml2.XMLHTTP");
}
if(request){
request.onreadystatechange=handleResponse;
request.open(reqType,url,true);
request.send(null);
}
}
This code fragment tests for the existence of the top-levelwindow objectActiveXObject, thus signaling the use of Internet Explorer. The code then initializes the request using two of a number of possibleActiveXprogram IDs (here,Microsoft.XMLHTTPandMsxml2.XMLHTTP).
You can get even more fine-grained when testing for different versions of the IE request object, such asMsxml2.XMLHTTP.4.0. In the vast majority of cases, however, you will not be designing your application based on various versions of the MSXML libraries, so the prior code will suffice.
The code then makes one final check for whether the request object has been properly constructed (if(request){...}).
Given three chances, if therequestvariable is stillnullorundefined, your browser is really out of luck when it comes to using the request object for Ajax!
Next: Ajax Basics continued >>
More JavaScript Articles
More By O'Reilly Media
|
This article is excerpted from the book Ajax Hacks, written by Bruce W. Perry (O'Reilly; ISBN: 0596101694). Check it out today at your favorite bookstore. Buy this book now.
|
|