<span style="font-family:Arial, Helvetica, sans-serif;">// <span style="color: rgb(51, 51, 51); font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 24px; background-color: rgb(245, 245, 245);">兼容 IE、Firefox、Chrome、Safari、Opera 等浏览器的 XML 文件加载方式</span></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><script type="text/javascript"></span>
var xmlDoc = null, xmlhttp = null; function loadXML() { xmlhttp = window.XMLHttpRequest ? new window.XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHttp"); if (xmlhttp == null) { alert("你的浏览器不支持 XMLHttpRequest"); return; } xmlhttp.open("GET", "1.xml?" + Date.parse(new Date()), true); xmlhttp.setRequestHeader("Content-Type", "text/xml"); xmlhttp.onreadystatechange = getmessage; xmlhttp.send(null); } function getmessage() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { xmlDoc = xmlhttp.responseXML.documentElement; if (xmlDoc == null) { alert("返回的数据不正确。"); return; } var nodes = xmlDoc.getElementsByTagName("t1") tb = document.getElementById("table_note"); tbody = document.createElement("tbody") for (i = 0; i < nodes.length; i++) { tr = document.createElement("tr") td = document.createElement("td") td.innerHTML = nodes[i].getElementsByTagName("title")[0].childNodes[0].nodeValue tr.appendChild(td) td = document.createElement("td") url = nodes[i].getElementsByTagName("url")[0].childNodes[0].nodeValue; td.innerHTML = "<a href='" + url + "'>" + url + "</a>" tr.appendChild(td) tbody.appendChild(tr) } tb.appendChild(tbody) } } </script> </head> <body onload="loadXML()"> <table id="table_note" border="1"> <tr> <td>姓名</td> <td>网址</td> </tr> </table> </body> </html>
第二种:注意chrome不支持
function loadXMLDoc(dname) { try //Internet Explorer { xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); } catch(e) { try //Firefox, Mozilla, Opera, etc. { xmlDoc=document.implementation.createDocument("","",null); } catch(e) {alert(e.message)} } try { xmlDoc.async=false; xmlDoc.load(dname); return(xmlDoc); } catch(e) {alert(e.message)} return(null); }