In turn, this means that you have to do a little bit of browser feature detection before you can etermine
in which way the object needs to be created. Because ActiveX controls and objects are unique to IE, you
can test for them by attempting to call the ActiveXObject method of the window object. For IE 7,
Mozilla Firefox, Safari, and Opera, there is an XMLHttpRequest method. These calls use implicit type
conversion to return true or false, depending on which browser is being used to view the page. In
other words, if there is an ActiveXObject on that browser, it will return true. If not, it will return
false.Typically your browser feature detection and object creation code would look something like this:
var xHRObject = false;
if (window.XMLHttpRequest)
{
xHRObject = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
xHRObject = new ActiveXObject(“Microsoft.XMLHTTP”);
}
else
{
//Do something to handle non-Ajax supporting browsers.
}
IE7之前,使用ActiveXObject,IE7之后用XMLHttpRequest。