Tuesday, October 10, 2006

[AJAX] IE 7 getting Native XMLHTTPRequest object

Programming AJAX Web application would be in trouble when users turning off the ActiveX support in IE. The sentence of

var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

would not be working. But luckily, there is native XMLHTTPRequest object like what non-IE browsers do.

var xmlHttp = new XMLHttpRequest();


No ActiveX object is needed, so far. Here is the code needs to be added to check the support of native XMLHTTPRequest object

if (window.XMLHttpRequest)
{
// If IE7, Mozilla, Safari, etc: Use native object
var xmlHttp = new XMLHttpRequest();
}
else
{
if (window.ActiveXObject){
// use the ActiveX control for IE5.x and IE6
var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}

No comments: