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

时间:2023-03-09 03:17:33
ajax 之js读取xml的多浏览器兼容

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

IE8以下只支持这种

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

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

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

部分代码如下:

 function Gett(str)
{ // 判断MyBrowserType,以决定读取text还textcontent。
var browsertype= checkBrowser();
var url="curr_run_xml.php";
// code for IE7+, Firefox, Chrome, Opera, Safari
try{ xmlHttp=new XMLHttpRequest();
xmlHttp.onreadystatechange=function()
{
if (xmlHttp.readyState==4 && xmlHttp.status==200)
{ xmlDoc=xmlHttp.responseXML;
InputVoltage.innerHTML = GetXmlTextByTagName(xmlDoc,"input_vol_a")
OutputVoltage.innerHTML = GetXmlTextByTagName(xmlDoc,"output_vol_a");
OutputMinVoltage.innerHTML = GetXmlTextByTagName(xmlDoc, "output_min_vol");
OutputMaxVoltage.innerHTML = GetXmlTextByTagName(xmlDoc, "output_max_vol");
Frequency.innerHTML = GetXmlTextByTagName(xmlDoc, "input_fre");
CurrentLoad.innerHTML = GetXmlTextByTagName(xmlDoc, "output_load_a"); <?php if(isset($rs))
{
foreach ($rs as $key => $value)
{
echo $value['dp_paramname'].".innerHTML=GetXmlTextByTagName(xmlDoc,'".$value['dp_paramname']."');\r\n";
}
} for($i=0;$i<count($resultHT);$i++)
{
$humidityVar = "td_humidity".$i;
$temperatureVar = "td_temperature".$i;
$ht_offline = "td_offline".$i;
echo $ht_offline.".innerHTML=GetXmlTextByTagName(xmlDoc,'".$ht_offline."');\r\n";
echo $humidityVar.".innerHTML=GetXmlTextByTagName(xmlDoc,'".$humidityVar."');\r\n";
echo $temperatureVar.".innerHTML=GetXmlTextByTagName(xmlDoc,'".$temperatureVar."');\r\n";
} ?> BatteryVoltage.innerHTML = GetXmlTextByTagName(xmlDoc,"batt_total_vol");
BatteryContent.innerHTML = GetXmlTextByTagName(xmlDoc,"batt_cap");
UPSCTemperature.innerHTML = GetXmlTextByTagName(xmlDoc,"ups_temp");
tfvar = GetXmlTextByTagName(xmlDoc,"ups_temp");
fTemp = parseFloat(tfvar);
fTemp = (fTemp*9)/5+32;
tfnum = fTemp.toFixed(2);
UPSFTemperature.innerHTML=tfnum.toString(); state1 = GetXmlTextByTagName(xmlDoc,"ws_value7");
state2 = GetXmlTextByTagName(xmlDoc,"ws_value4");
state3 = GetXmlTextByTagName(xmlDoc,"ws_value6");
state4 = GetXmlTextByTagName(xmlDoc,"ws_value5");
state5 = GetXmlTextByTagName(xmlDoc,"ws_value0");
state6 = GetXmlTextByTagName(xmlDoc,"ws_value1");
state7 = GetXmlTextByTagName(xmlDoc,"ws_value3");
state8 = GetXmlTextByTagName(xmlDoc,"ws_value2"); changeImage();
changeWord();
}
}
xmlHttp.open("GET",url,false);
xmlHttp.send(null);
}
catch (e)
{ xmlHttp = -1;
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");
for (var i = 0; i < XmlHttpVersions.length ; i++)
{
try {
if(xmlHttp = new ActiveXObject(XmlHttpVersions[i]))
{ break;
} }
catch (e)
{
continue;
}
} if (!xmlHttp) alert("获取浏览器xml控件失败"); xmlHttp.onreadystatechange=function()
{
if (xmlHttp.readyState==4 && xmlHttp.status==200)
{ xmlDoc=xmlHttp.responseXML;
InputVoltage.innerText = GetXmlTextByTagName(xmlDoc,"input_vol_a")
OutputVoltage.innerText = GetXmlTextByTagName(xmlDoc,"output_vol_a");
OutputMinVoltage.innerText = GetXmlTextByTagName(xmlDoc, "output_min_vol");
OutputMaxVoltage.innerText = GetXmlTextByTagName(xmlDoc, "output_max_vol");
Frequency.innerText = GetXmlTextByTagName(xmlDoc, "input_fre");
CurrentLoad.innerText = GetXmlTextByTagName(xmlDoc, "output_load_a"); <?php if(isset($rs))
{
foreach ($rs as $key => $value)
{
echo $value['dp_paramname'].".innerText=GetXmlTextByTagName(xmlDoc,'".$value['dp_paramname']."');\r\n";
}
} for($i=0;$i<count($resultHT);$i++)
{
$humidityVar = "td_humidity".$i;
$temperatureVar = "td_temperature".$i;
echo $humidityVar.".innerText=GetXmlTextByTagName(xmlDoc,'".$humidityVar."');\r\n";
echo $temperatureVar.".innerText=GetXmlTextByTagName(xmlDoc,'".$temperatureVar."');\r\n";
} ?> BatteryVoltage.innerText = GetXmlTextByTagName(xmlDoc,"batt_total_vol");
BatteryContent.innerText = GetXmlTextByTagName(xmlDoc,"batt_cap");
UPSCTemperature.innerText = GetXmlTextByTagName(xmlDoc,"ups_temp");
tfvar = GetXmlTextByTagName(xmlDoc,"ups_temp");
fTemp = parseFloat(tfvar);
fTemp = (fTemp*9)/5+32;
tfnum = fTemp.toFixed(2);
UPSFTemperature.innerText=tfnum.toString(); state1 = GetXmlTextByTagName(xmlDoc,"ws_value7");
state2 = GetXmlTextByTagName(xmlDoc,"ws_value4");
state3 = GetXmlTextByTagName(xmlDoc,"ws_value6");
state4 = GetXmlTextByTagName(xmlDoc,"ws_value5");
state5 = GetXmlTextByTagName(xmlDoc,"ws_value0");
state6 = GetXmlTextByTagName(xmlDoc,"ws_value1");
state7 = GetXmlTextByTagName(xmlDoc,"ws_value3");
state8 = GetXmlTextByTagName(xmlDoc,"ws_value2"); changeImage();
changeWord(); xmlHttp = null;
}
} xmlHttp.open("GET",url,false);
xmlHttp.send(null); } setTimeout("Gett()",time_inteval);
}
 function check(reg) {
var ug = navigator.userAgent.toLowerCase();
return reg.test(ug);
} function checkBrowser()
{
var ug = navigator.userAgent.toLowerCase();
// var userAgent = document.getElementById("userAgent");
// userAgent.innerHTML = "浏览器的用户代理报头:" + ug; //alert("浏览器的用户代理报头:" + ug); var browserType = "";
var ver = ""; //检测IE及版本
var IE = ug.match(/msie\s*\d\.\d/); //提取浏览器类型及版本信息,注match()方法返回的是数组而不是字符串
var isIE = check(/msie/);
if(isIE) {
browserType = "Internet Explorer";
ver = IE.join(" ").match(/[0-9]/g).join("."); //先用join()方法转化为字符串,然后用match()方法匹配到版本信息,再用join()方法转化为字符串 MyBrowserType=1;
} //检测chrome及版本
var chrome = ug.match(/chrome\/\d\.\d/gi);
var isChrome = check(/chrome/);
if(isChrome) {
browserType = "Chrome";
//ver = chrome.join(" ").match(/[0-9]/g).join(".");
MyBrowserType=2; } //检测firefox及版本
var firefox = ug.match(/firefox\/\d\.\d/gi);
var isFirefox = check(/firefox/);
if(isFirefox) {
browserType = "Firefox";
ver = firefox.join(" ").match(/[0-9]/g).join(".");
MyBrowserType=3;
}
return browserType;
}
 function GetXmlTextByTagName(xmlDoc,id)
{
var Obj, szTxt = "";
try
{
Obj = xmlDoc.getElementsByTagName(id);
if (typeof(Obj) == "undefined")
{
return szTxt;
}
else if ((Obj = Obj.item(0)) == null)
{
return szTxt;
}
else
{ if(MyBrowserType==1)
szTxt = Obj.text;
else
szTxt = Obj.textContent; }
}
catch (objException)
{
}
return szTxt;
}