【XMLHttpRequest对象】

<script type="text/javascript">

//创建浏览器兼容的XMLHttpRequest对象

//方法一
var Try ={
these:function(){
    var returnValue;
    for(var i=0;i<arguments.length;i++){
       var lambda=arguments[i];
       try{
         returnValue=lambda();
         break;
       }catch(e){}
    }
    return returnValue;
  }
}
//创建XMLHttp对象
var xmlhttp =Try.these(
   function() {return new XMLHttpRequest()},
   function() {return new ActiveXObject('Msxml2.XMLHTTP')},
   function() {return new ActiveXObject('Microsoft.XMLHTTP')}
   ) || false;


//方法二
var xmlhttp;
try{ 

     xmlhttp= new ActiveXObject('Msxml2.XMLHTTP');

}catch(e){

     try{ xmlhttp= new ActiveXObject('Microsoft.XMLHTTP');}

          catch(e){
               try{
                    xmlhttp= new XMLHttpRequest();
               }catch(e){}

          }

}

 

//定义XMLHttpRequest对象的事件处理程序

xmlhttp.onreadystatechange=function(){
     if(xmlhttp.readyState==4){
          if(xmlhttp.status==200){
               //获取xml对象
                  var xmlObj=xmlhttp.responseXML;
                  //获取<title>结点的值
                  var title=xmlObj.getElementsByTagName("title")[0].text;
                  document.write(title);
          }else{
                    document.write(xmlhttp.status);
          }
     }

}

 

//创建一个请求
xmlhttp.open("get","a.xml");

//发送请求
xmlhttp.send(null);


</script>

posted on 2008-08-12 15:47  ....11  阅读(106)  评论(0编辑  收藏  举报

导航