引用:http://www.cnblogs.com/whyoop/archive/2011/03/01/1968405.html
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;
}
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;
}
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;
}