为了使前一页中的代码易于维护(检查旧的浏览器),它应该写成一个函数:
function loadXMLDoc(dname) |
{ |
if (window.XMLHttpRequest) |
{ |
xhttp=new XMLHttpRequest(); |
} |
else |
{ |
xhttp=new ActiveXObject("Microsoft.XMLHTTP"); |
} |
xhttp.open("GET",dname,false); |
xhttp.send(); |
return xhttp.responseXML; |
} |
上面的函数可以存储在 HTML 页面的部分,并从页面中的脚本调用。
上面描述的函数,用于本教程中所有 XML 文档实例!
为了使上述代码更容易维护,以确保在所有页面中使用相同的代码,我们把函数存储在一个外部文件中。
文件名为 "loadxmldoc.js",且在 HTML 页面中的 head 部分被加载。然后,页面中的脚本调用 loadXMLDoc() 函数。
下面的实例使用 loadXMLDoc() 函数加载 books.xml:
<html> |
<head> |
<script src="loadxmldoc.js"> |
</script> |
</head> |
<body> |
<script> |
xmlDoc=loadXMLDoc("books.xml"); |
code goes here..... |
</script> |
</body> |
</html> |
为了使前一页中的代码易于维护(检查旧的浏览器),它应该写成一个函数:
function loadXMLString(txt) |
{ |
if (window.DOMParser) |
{ |
parser=new DOMParser(); |
xmlDoc=parser.parseFromString(txt,"text/xml"); |
} |
else // Internet Explorer |
{ |
xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); |
xmlDoc.async=false; |
xmlDoc.loadXML(txt); |
} |
return xmlDoc; |
} |
上面描述的函数,用于本教程中所有 XML 字符串实例!
我们已经把 loadXMLString() 函数存储在名为 "loadxmlstring.js" 文件中。
<html> |
<head> |
<script src="loadxmlstring.js"></script> |
</head> |
<body> |
<script> |
text="<bookstore>" |
text=text+"<book>"; |
text=text+"<title>Everyday Italian</title>"; |
text=text+"<author>Giada De Laurentiis</author>"; |
text=text+"<year>2005</year>"; |
text=text+"</book>"; |
text=text+"</bookstore>"; |
xmlDoc=loadXMLString(text); |
code goes here..... |
</script> |
</body> |
</html> |