However, Internet Explorer 6 and its older versions won??™t recognize the XMLHttpRequest object,
an exception will be generated, and the execution will be passed to the catch block. For Internet
Explorer 6 and older versions, the XMLHttpRequest object needs to be created as an ActiveX
control:
catch(e)
{
// Assume IE6 or older
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
}
catch(e) { }
}
There are more techniques for creating the XMLHttpRequest object. For example, another
technique is to use a JavaScript feature called object detection. This feature allows you to check
whether a particular object is supported by the browser, and it works like this:
if (window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
At the end of createXmlHttpRequestObject, we test that xmlHttp contains a valid
XMLHttpRequest instance:
// Return the created object or display an error message
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
Here we used the reverse effect of JavaScript??™s object detection feature, which says that
JavaScript will evaluate a valid object instance, such as xmlHttp, to true. The negation of this
expression??”!xmlHttp??”returns true if xmlHttp is false, null, or undefined. (In JavaScript,
undefined is a special ???value??? that is automatically assigned to a variable or object that has not
been assigned a value.
Pages:
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518