最近开始学《JavaScript DOM 编程艺术》 其中有一个脚本文件是这样的
function getHTTPObject()
{
if (typeof XMLHttpRequest == "undefined")
{
XMLHttpRequest = function()
{
try{return new ActiveXObject ("Msxml2.XMLHTTP.6.0");}
catch (e){}
try{return new ActiveXObject ("Msxml2.XMLHTTP.3.0");}
catch (e){}
try{return new ActiveXObject ("Msxml2.XMLHTTP");}
catch (e){}
try{return new ActiveXObject ("Microsoft.XMLHTTP");}
catch (e){}
return false;
}
return new XMLHttpRequest();
<span style="white-space:pre"> </span>}
}
但经过实际测试,此功能并不能使用,原因可能是浏览器已经定义了XMLHttpRequest
所以解决的办法很简单。。。
加入一个ELSE return语句
function getHTTPObject()
{
if (typeof XMLHttpRequest == "undefined")
{
XMLHttpRequest = function()
{
try{return new ActiveXObject ("Msxml2.XMLHTTP.6.0");}
catch (e){}
try{return new ActiveXObject ("Msxml2.XMLHTTP.3.0");}
catch (e){}
try{return new ActiveXObject ("Msxml2.XMLHTTP");}
catch (e){}
try{return new ActiveXObject ("Microsoft.XMLHTTP");}
catch (e){}
return false;
}
return new XMLHttpRequest();
}
else
{
return new XMLHttpRequest();
}
}
经测试,正常了。。。
仅供广大书友参考