为什么javascript加载xml文件,在Chrome下不成功!

转自:http://www.iteye.com/problems/65523


function loadDataXML(sFileName) 


var sCurrentDocPath=_getPath(document.location.href); 
sdocPath=_getFullPath(sCurrentDocPath,sFileName); 
if(gbIE5) 

xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); 
xmlDoc.async=true; 
xmlDoc.onreadystatechange=checkState; 
if(document.body!=null) 
xmlDoc.load(sdocPath); 

else if(gbNav6) 

xmlDoc=document.implementation.createDocument("","",null); 
xmlDoc.addEventListener("load",initializeData,false); 
xmlDoc.load(sdocPath,"text/xml"); 


这段代码在ff和ie是正常的,但是chrome报错了,Uncaught TypeError: Object #<Document> has no method 'load' 
求教各位应该怎么写才对啊?

------------------------------------------------------------------------------------------------------------------
问题补充:
dream_hanker 写道
Chrome浏览器没有实现XMLdocument.load方法,你可以换成XmlHttpRequest 
下面是一段实例代码 
Javascript代码   收藏代码
  1. try //Internet Explorer  
  2. {  
  3.  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");  
  4.  xmlDoc.async=false;  
  5.  xmlDoc.load(file);  
  6. }  
  7. catch(e)  
  8. {  
  9.  try //Firefox, Mozilla, Opera, etc.  
  10.  {  
  11.   xmlDoc=document.implementation.createDocument("","",null);  
  12.   xmlDoc.async=false;  
  13.   xmlDoc.load(file);  
  14.  }  
  15.  catch(e)  
  16.  {  
  17.   try //Google Chrome  
  18.   {  
  19.    var xmlhttp = new window.XMLHttpRequest();  
  20.    xmlhttp.open("GET",file,false);  
  21.    xmlhttp.send(null);  
  22.    xmlDoc = xmlhttp.responseXML.documentElement;  
  23.   }  
  24.   catch(e)  
  25.   {  
  26.    error=e.message;  
  27.   }  
  28.  }  
  29. }  



dream_hanker 写道
Chrome浏览器没有实现XMLdocument.load方法,你可以换成XmlHttpRequest 
下面是一段实例代码 
Javascript代码   收藏代码
  1. try //Internet Explorer  
  2. {  
  3.  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");  
  4.  xmlDoc.async=false;  
  5.  xmlDoc.load(file);  
  6. }  
  7. catch(e)  
  8. {  
  9.  try //Firefox, Mozilla, Opera, etc.  
  10.  {  
  11.   xmlDoc=document.implementation.createDocument("","",null);  
  12.   xmlDoc.async=false;  
  13.   xmlDoc.load(file);  
  14.  }  
  15.  catch(e)  
  16.  {  
  17.   try //Google Chrome  
  18.   {  
  19.    var xmlhttp = new window.XMLHttpRequest();  
  20.    xmlhttp.open("GET",file,false);  
  21.    xmlhttp.send(null);  
  22.    xmlDoc = xmlhttp.responseXML.documentElement;  
  23.   }  
  24.   catch(e)  
  25.   {  
  26.    error=e.message;  
  27.   }  
  28.  }  
  29. }  





老大,不行啊!能不能照着我贴的代码帮我改下啊!急啊

------------------------------------------------------------------------------------------------------------------
问题补充:
hudingchen 写道
你把代码贴全了阿

我倒,这还不全啊???

------------------------------------------------------------------------------------------------------------------
问题补充:
问题已经解决,原因是:在判断是chrome浏览器的时候没有解析只是去得到了该xml对象

该问题已经关闭: 问题已经解决,原因是:在判断是chrome浏览器的时候没有解析只是去得到了该xml对象


/

//MGQ

改了还是不行。

在Chrome还是不能运行

错误:57XMLHttpRequest cannot load file:///E:/%E6%AF%95%E4%B8%9A%E8%AE%BE%E8%AE%A1/%E7%AC%94%E8%AE%B0/2011-11-26%E7%BD%91%E9%A1%B5%E6%B5%8B%E8%AF%95/measure02.xml. Cross origin requests are only supported for HTTP.


源代码:::

<html>
<head>
<script type="text/javascript">
var xmlDoc;
function loadXML()
{
// 用于 IE 的代码:
/*if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("measure02.xml");
getmessage();
}
// 用于 Mozilla, Firefox, Opera, 等浏览器的代码:
else if (document.implementation &&
document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("measure02.xml");
xmlDoc.οnlοad=getmessage;
}
else
{
alert('Your browser cannot handle this script');
}
*/

try //Internet Explorer  
{  
 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");  
 xmlDoc.async=false;  
 xmlDoc.load("measure02.xml");  
 getmessage();
}  
catch(e)  
{  
 try //Firefox, Mozilla, Opera, etc.  
 {  
  xmlDoc=document.implementation.createDocument("","",null);  
  xmlDoc.async=false;  
  xmlDoc.load("measure02.xml");  
  xmlDoc.οnlοad=getmessage;
 }  
 catch(e)  
 {  
  try //Google Chrome  
  {  
   var xmlhttp = new window.XMLHttpRequest();  
   xmlhttp.open("GET","measure02.xml",false);  
   xmlhttp.send(null);  
   xmlDoc = xmlhttp.responseXML.documentElement;  
  }  
  catch(e)  
  {  
   error=e.message;  
  }  
 }  
} 

setTimeout("loadXML()",100);
}
function getmessage()
{
document.getElementById("ch_one").innerHTML=
xmlDoc.getElementsByTagName("ch_one")[0].childNodes[0].nodeValue;
document.getElementById("ch_two").innerHTML=
xmlDoc.getElementsByTagName("ch_two")[0].childNodes[0].nodeValue;
document.getElementById("ch_three").innerHTML=
xmlDoc.getElementsByTagName("ch_three")[0].childNodes[0].nodeValue;
document.getElementById("ch_four").innerHTML=
xmlDoc.getElementsByTagName("ch_four")[0].childNodes[0].nodeValue;
}
</script>
</head>
<body οnlοad="loadXML()">
<h1>ARM9 远程实时测量技术</h1>
<form name="loandata">
<table border="8">
<tr><td colspan=3> <big> <b> 温度测量点(1-4): </b> </big> </td> </tr>
<tr>
<td>1) </td>
<td> Channel one </td>
<td><span id="ch_one"> </span>℃</td>
</tr>
<tr>
<td>2) </td>
<td> Channel two </td>
<td><span id="ch_two"> </span>℃</td>
</tr>
<tr>
<td>3)</td>
<td> Channel three</td>
<td><span id="ch_three"> </span>℃</td>
</tr>
<tr>
<td>4)</td>
<td> Channel four</td>
<td><span id="ch_four"> </span>℃</td>
</tr>
</table>
</form>
</body>
</html>




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值