Creating the XMLHttpRequest Object
In Internet Explorer 6 and older, XMLHttpRequest is implemented as an ActiveX control, and
you create it like this:
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
In Internet Explorer 7 and other web browsers??”including Firefox, Opera, and Safari??”
XMLHttpRequest is a native object, so you create instances of it like this:
xmlHttp = new XMLHttpRequest();
To create XMLHttpRequest objects for all browsers, we??™ll use the following JavaScript function,
which creates an XMLHttpRequest instance by using the native object if available or the
Microsoft.XMLHttp ActiveX control for visitors who use Internet Explorer 6 or older:
// Creates an XMLHttpRequest instance
function createXmlHttpRequestObject()
{
// xmlHttp will store the reference to the XMLHttpRequest object
var xmlHttp;
// Try to instantiate the native XMLHttpRequest object
try
{
// Create an XMLHttpRequest object
xmlHttp = new XMLHttpRequest();
}
catch(e)
{
// Assume IE6 or older
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
}
catch(e) { }
}
// Return the created object or display an error message
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
CHAPTER 13 ?– IMPLEMENTING AJAX FEATURES 402
?– Note Microsoft.XMLHttp is the oldest version of the ActiveX XMLHttp library, but the library comes in
many more flavors and versions than you could imagine.
Pages:
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515