Mozilla Firefox与IE浏览器中用AJAX处理XML文件的通用代码

//
//加载XML文件
//


var xmlDoc;
 function loadXML(xmlfile){
    //load xml file
    // code for IE
    if (window.ActiveXObject)
    {
        xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async=false;
        xmlDoc.load(xmlfile);
    }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation && document.implementation.createDocument)
    {
        xmlDoc=document.implementation.createDocument("","",null);
        xmlDoc.load(xmlfile);   
    }
    else
    {
        alert('Your browser cannot handle this script');
    }
}

//
//下载通用代码
//

var xmlHttp;    /*XMLHttpRequest对象 */
function createXMLHttpRequest(){
    if (window.ActiveXObject){    /*在IE下初始化XMLHttpRequest对象 */
        try{
              //新版本的 Internet Explorer
              xmlHttp= new ActiveXObject("Msxml2.XMLHTTP");
        }catch (otherMicrosoft){
              try {
                   //较老版本的 Internet Explorer
                  xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
              } catch (failed){
                   // 还是失败,那么就认为创建失败……
                   xmlHttp= false;
               }            
    }  else if(window.XMLHttpRequest){    /*在Firefox下初始化XMLHttpRequest对象 */
        xmlHttp = new XMLHttpRequest();
    }
   if(!xmlHttp)
      alert("创建 XMLHttpRequest 对象失败!");
}
 
function startRequest(doUrl){
    createXMLHttpRequest(); 
    xmlHttp.onreadystatechange = handleStateChange; /* 设置回调函数为handleStateChange. */
    xmlHttp.open("GET", doUrl, true); /*通过get方法来请求目标
doUrl 并且设置为异步完成请求*/
    xmlHttp.send(null);
}
 
function handleStateChange(){    /*XMLHttpRequest对象内部状态每次有变化时候都会调用 handleStateChange 函数。*/
    if (xmlHttp.readyState == 4 ){ /*请求完成*/
        if (xmlHttp.status == 200 ){ /*服务器的HTTP状态为200,既OK */
             // 一切OK,调用处理过程
             //    DoMyXML();
         }else if(XMLHttp.status == 404){
             //文件不存在
             alert("Requested URL is not found.");
        }else if(XMLHttp.status == 403){
            //没有权限
            alert("Access denied.");
       }else
          alert("status is " + XMLHttp.status);
      }
    }
}
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值