兼容XMLHttpRequest实例化函数.

function getXmlHttp(){
try
{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
xmlhttp = null;
}
}
if (xmlhttp==null && typeof XMLHttpRequest!="undefined"){
try
{
xmlhttp=new XMLHttpRequest();
xmlhttp.overrideMimeType('text/xml');
}catch(e){
xmlhttp=false;
}
}
return xmlhttp;
}

以上函数是我最近一次实例XMLHttpRequest的方法.

早先进行实例的时候,是先处理支持XMLHttpRequest类的标准浏览器,再处理IE这种使用ActiveX实现XMLHttpRequest.

但是前几天发现,似乎IE7已经开始支持XMLHttpRequest,但是使用工程中有发现有点问题,就有改了一下,使用上面这个函数来获取XMLHttpRequest支持.

Tags: , , ,

Friday, May 30th, 2008 w3c

3 Comments to 兼容XMLHttpRequest实例化函数.

  1. 写代码不缩进的家伙…
    if (!window.XMLHttpRequest) {
    window.XMLHttpRequest=function (){
    return new ActiveXObject(”Microsoft.XMLHTTP”);
    }
    }

  2. BlueF on May 30th, 2008
  3. 不是我不缩进…

    code里自己把空格过滤掉了,我又懒的去替换成空格实体.

  4. Sonic on May 30th, 2008
  5. 儿童节快乐~~

    好像和你的文章无关……

    不过算了
    高高兴兴过节去~

  6. kingki on June 1st, 2008

Leave a comment