谷歌浏览器加载XML文件的问题

W3CSchool的教程中提供过一个loadXMLDoc函数:
 
   
function loadXMLDoc(url) {
var xmlDoc;
try {
xmlDoc
= new ActiveXObject( " Microsoft.XMLDOM " );
}
catch (e){
try {
xmlDoc
= document.implementation.createDocument( "" , "" , null );
}
catch (e){
alert(e.message);
return ;
}
}
xmlDoc.async
= false ;
xmlDoc.load(url);
return xmlDoc;
}

不过在谷歌浏览器中会出现如下错误:
Object #<a Document> has no method 'load'
所以为了适应谷歌浏览器只能用XMLHttpRequest对像再responseXML得到XML
修改后的loadXMLDoc函数如下:
 
   
function loadXMLDoc(url) {
var xmlDoc;
try {
xmlDoc
= new ActiveXObject( " Microsoft.XMLDOM " );
}
catch (e){
try {
var oXmlHttp = new XMLHttpRequest() ;
oXmlHttp.open(
" GET " , url, false ) ;
oXmlHttp.send(
null ) ;
return oXmlHttp.responseXML;
}
catch (e){
return ;
}
}
xmlDoc.async
= false ;
xmlDoc.load(url);
return xmlDoc;
}

你可能感兴趣的:(xml)