ajax 之js读取xml的多浏览器兼容

主要是分为两大类:IE、其它浏览器

IE8以下只支持这种

InputVoltage.innerText = xmlDoc.getElementsByTagName(id)[0].text,

其它浏览器大部分兼容这种

 InputVoltage.innerHTML =xmlDoc.getElementsByTagName(id)[0].textContent。

 

部分代码如下:

  1 function Gett(str)

  2 {

  3  

  4   // 判断MyBrowserType,以决定读取text还textcontent。

  5   var browsertype= checkBrowser();

  6    var url="curr_run_xml.php";  

  7          // code for IE7+, Firefox, Chrome, Opera, Safari  

  8           try{

  9 

 10          xmlHttp=new XMLHttpRequest();     

 11          xmlHttp.onreadystatechange=function()

 12          {

 13              if (xmlHttp.readyState==4 && xmlHttp.status==200)

 14              {

 15 

 16                     xmlDoc=xmlHttp.responseXML;                        

 17                     InputVoltage.innerHTML =  GetXmlTextByTagName(xmlDoc,"input_vol_a")      

 18                     OutputVoltage.innerHTML = GetXmlTextByTagName(xmlDoc,"output_vol_a");       

 19                     OutputMinVoltage.innerHTML = GetXmlTextByTagName(xmlDoc, "output_min_vol");  

 20                     OutputMaxVoltage.innerHTML = GetXmlTextByTagName(xmlDoc, "output_max_vol"); 

 21                     Frequency.innerHTML = GetXmlTextByTagName(xmlDoc, "input_fre");   

 22                     CurrentLoad.innerHTML = GetXmlTextByTagName(xmlDoc, "output_load_a");

 23 

 24                       <?php 

 25 

 26                         if(isset($rs))

 27                         {

 28                            foreach ($rs as $key => $value) 

 29                            {

 30                               echo  $value['dp_paramname'].".innerHTML=GetXmlTextByTagName(xmlDoc,'".$value['dp_paramname']."');\r\n";

 31                            }

 32                         }

 33 

 34                         for($i=0;$i<count($resultHT);$i++)

 35                         {

 36                             $humidityVar = "td_humidity".$i;

 37                             $temperatureVar = "td_temperature".$i;

 38                             $ht_offline = "td_offline".$i;

 39                             echo  $ht_offline.".innerHTML=GetXmlTextByTagName(xmlDoc,'".$ht_offline."');\r\n";

 40                             echo  $humidityVar.".innerHTML=GetXmlTextByTagName(xmlDoc,'".$humidityVar."');\r\n";

 41                             echo  $temperatureVar.".innerHTML=GetXmlTextByTagName(xmlDoc,'".$temperatureVar."');\r\n";

 42                         }

 43 

 44                       ?>

 45 

 46               

 47                     BatteryVoltage.innerHTML = GetXmlTextByTagName(xmlDoc,"batt_total_vol");

 48                     BatteryContent.innerHTML = GetXmlTextByTagName(xmlDoc,"batt_cap");

 49                     UPSCTemperature.innerHTML = GetXmlTextByTagName(xmlDoc,"ups_temp");

 50                       tfvar = GetXmlTextByTagName(xmlDoc,"ups_temp");

 51                       fTemp = parseFloat(tfvar);

 52                       fTemp = (fTemp*9)/5+32;

 53                       tfnum = fTemp.toFixed(2); 

 54                     UPSFTemperature.innerHTML=tfnum.toString();

 55                   

 56                   

 57                      state1 = GetXmlTextByTagName(xmlDoc,"ws_value7");

 58                      state2 = GetXmlTextByTagName(xmlDoc,"ws_value4");

 59                      state3 = GetXmlTextByTagName(xmlDoc,"ws_value6");

 60                      state4 = GetXmlTextByTagName(xmlDoc,"ws_value5");

 61                      state5 = GetXmlTextByTagName(xmlDoc,"ws_value0");

 62                      state6 = GetXmlTextByTagName(xmlDoc,"ws_value1");

 63                      state7 = GetXmlTextByTagName(xmlDoc,"ws_value3");

 64                      state8 = GetXmlTextByTagName(xmlDoc,"ws_value2");

 65 

 66                     changeImage();

 67                     changeWord();    

 68              }

 69            }

 70            xmlHttp.open("GET",url,false);  

 71            xmlHttp.send(null);  

 72          }

 73          catch (e) 

 74           {

 75 

 76                       xmlHttp = -1;

 77                       var XmlHttpVersions = new Array("Microsoft.XMLDOM,MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP","Microsoft.XMLHTTP","MSXML4.DOMDocument", "MSXML3.DOMDocument", "MSXML2.DOMDocument", "MSXML.DOMDocument");

 78                       for (var i = 0; i < XmlHttpVersions.length ; i++) 

 79                       {

 80                         try {

 81                               if(xmlHttp = new ActiveXObject(XmlHttpVersions[i]))

 82                               {

 83                     

 84                               break;

 85                               }

 86                             

 87                          }

 88                       catch (e) 

 89                        {

 90                          continue;

 91                        }

 92                   }

 93     

 94 

 95 

 96           if (!xmlHttp)  alert("获取浏览器xml控件失败");

 97  

 98            xmlHttp.onreadystatechange=function()  

 99               {  

100                 if (xmlHttp.readyState==4  && xmlHttp.status==200)  

101                     {  

102                       

103                         xmlDoc=xmlHttp.responseXML;                        

104                         InputVoltage.innerText =  GetXmlTextByTagName(xmlDoc,"input_vol_a")      

105                         OutputVoltage.innerText = GetXmlTextByTagName(xmlDoc,"output_vol_a");       

106                         OutputMinVoltage.innerText = GetXmlTextByTagName(xmlDoc, "output_min_vol");  

107                         OutputMaxVoltage.innerText = GetXmlTextByTagName(xmlDoc, "output_max_vol"); 

108                         Frequency.innerText = GetXmlTextByTagName(xmlDoc, "input_fre");   

109                         CurrentLoad.innerText = GetXmlTextByTagName(xmlDoc, "output_load_a");

110 

111                           <?php 

112 

113                             if(isset($rs))

114                             {

115                                foreach ($rs as $key => $value) 

116                                {

117                                   echo  $value['dp_paramname'].".innerText=GetXmlTextByTagName(xmlDoc,'".$value['dp_paramname']."');\r\n";

118                                }

119                             }

120 

121                             for($i=0;$i<count($resultHT);$i++)

122                             {

123                                 $humidityVar = "td_humidity".$i;

124                                 $temperatureVar = "td_temperature".$i;

125                                 echo  $humidityVar.".innerText=GetXmlTextByTagName(xmlDoc,'".$humidityVar."');\r\n";

126                                 echo  $temperatureVar.".innerText=GetXmlTextByTagName(xmlDoc,'".$temperatureVar."');\r\n";

127                             }

128 

129                           ?>

130 

131                         BatteryVoltage.innerText = GetXmlTextByTagName(xmlDoc,"batt_total_vol");

132                         BatteryContent.innerText = GetXmlTextByTagName(xmlDoc,"batt_cap");

133                         UPSCTemperature.innerText = GetXmlTextByTagName(xmlDoc,"ups_temp");

134                           tfvar = GetXmlTextByTagName(xmlDoc,"ups_temp");

135                           fTemp = parseFloat(tfvar);

136                           fTemp = (fTemp*9)/5+32;

137                           tfnum = fTemp.toFixed(2); 

138                         UPSFTemperature.innerText=tfnum.toString();

139                       

140                       

141                          state1 = GetXmlTextByTagName(xmlDoc,"ws_value7");

142                          state2 = GetXmlTextByTagName(xmlDoc,"ws_value4");

143                          state3 = GetXmlTextByTagName(xmlDoc,"ws_value6");

144                          state4 = GetXmlTextByTagName(xmlDoc,"ws_value5");

145                          state5 = GetXmlTextByTagName(xmlDoc,"ws_value0");

146                          state6 = GetXmlTextByTagName(xmlDoc,"ws_value1");

147                          state7 = GetXmlTextByTagName(xmlDoc,"ws_value3");

148                          state8 = GetXmlTextByTagName(xmlDoc,"ws_value2");

149 

150                         changeImage();

151                         changeWord();   

152 

153                         xmlHttp = null;  

154                     }            

155               }  

156                 

157              xmlHttp.open("GET",url,false);  

158              xmlHttp.send(null);  

159 

160 

161             

162       }

163             

164         

165         setTimeout("Gett()",time_inteval);

166 }    
 1 function check(reg) {

 2     var ug = navigator.userAgent.toLowerCase();

 3     return reg.test(ug);

 4  }

 5  

 6  function checkBrowser() 

 7  {

 8       var ug = navigator.userAgent.toLowerCase();

 9     //  var userAgent = document.getElementById("userAgent");

10     //  userAgent.innerHTML = "浏览器的用户代理报头:" + ug;

11         

12         //alert("浏览器的用户代理报头:" + ug);

13 

14       var browserType = "";

15       var ver = "";

16       

17       //检测IE及版本

18       var IE = ug.match(/msie\s*\d\.\d/); //提取浏览器类型及版本信息,注match()方法返回的是数组而不是字符串

19       var isIE = check(/msie/);

20       if(isIE) {

21        browserType = "Internet Explorer";

22        ver = IE.join(" ").match(/[0-9]/g).join("."); //先用join()方法转化为字符串,然后用match()方法匹配到版本信息,再用join()方法转化为字符串

23        

24        MyBrowserType=1;

25       }

26       

27       //检测chrome及版本

28       var chrome = ug.match(/chrome\/\d\.\d/gi);

29       var isChrome = check(/chrome/);

30       if(isChrome) {

31        browserType = "Chrome";

32        //ver = chrome.join(" ").match(/[0-9]/g).join(".");

33        MyBrowserType=2;

34 

35       }

36       

37       //检测firefox及版本

38       var firefox = ug.match(/firefox\/\d\.\d/gi);

39       var isFirefox = check(/firefox/);

40       if(isFirefox) {

41        browserType = "Firefox";

42        ver = firefox.join(" ").match(/[0-9]/g).join(".");  

43        MyBrowserType=3; 

44   }

45      return browserType;

46  }
 1 function GetXmlTextByTagName(xmlDoc,id)

 2 {

 3     var Obj, szTxt = "";

 4     try

 5     {

 6         Obj = xmlDoc.getElementsByTagName(id);

 7         if (typeof(Obj) == "undefined")

 8         {

 9             return szTxt;

10         }

11         else if ((Obj = Obj.item(0)) == null)

12         {

13             return szTxt;

14         }

15         else

16         {

17     

18             if(MyBrowserType==1)

19                 szTxt = Obj.text;

20             else

21                 szTxt = Obj.textContent;

22                 

23         }

24     }

25     catch (objException)

26     {

27     }

28     return szTxt;

29 }

 

你可能感兴趣的:(浏览器兼容)