<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
var strLocal="http://192.168.1.105/demo/";
function processRequest() {
var tran;
if (httpRequest.readyState == 4) { // 判断对象状态
if (httpRequest.status == 200) { // 信息已成功返回,开始处理信息
tran = httpRequest.responseXML;
readNewRecord(tran);
} else { // 页面不正常
alert("您所请求的页面发生错误!");
}
}
}
function sendRequest() {
// 初始化,指定处理函数
httpRequest = false;
// 开始初始化xmlhttprequest对象
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType) {
httpRequest.overrideMimeType("text/xml");
}
} else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!httpRequest) { // 异常处理
window.alert("不能创建XMLHttpRequest对象实例");
return false;
}
httpRequest.onreadystatechange = processRequest;
// 确定发送的方式和URL以及是否同步执行代码
httpRequest.open("GET", strLocal+"bb.xml", true);
httpRequest.send(null);
}
function readNewRecord(strdata){
var tempOut="";
var cateSet=strdata.getElementsByTagName("student"); //line43
for(var idx=0;idx<cateSet.length;idx++){
tempOut+="<dl><dt>"+cateSet[idx].getElementsByTagName("name")[0].childNodes[0].nodeValue+"</dt><dd>"+cateSet[idx].getElementsByTagName("sex")
[0].childNodes[0].nodeValue+"</dd></dl>";
}
document.getElementById("view").innerHTML=tempOut;
}
</script>
</head><body onload="sendRequest();">
<div id="view"></div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript">
var strLocal="http://192.168.1.105/demo/";
function processRequest() {
var tran;
if (httpRequest.readyState == 4) { // 判断对象状态
if (httpRequest.status == 200) { // 信息已成功返回,开始处理信息
tran = httpRequest.responseXML;
readNewRecord(tran);
} else { // 页面不正常
alert("您所请求的页面发生错误!");
}
}
}
function sendRequest() {
// 初始化,指定处理函数
httpRequest = false;
// 开始初始化xmlhttprequest对象
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
if (httpRequest.overrideMimeType) {
httpRequest.overrideMimeType("text/xml");
}
} else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!httpRequest) { // 异常处理
window.alert("不能创建XMLHttpRequest对象实例");
return false;
}
httpRequest.onreadystatechange = processRequest;
// 确定发送的方式和URL以及是否同步执行代码
httpRequest.open("GET", strLocal+"bb.xml", true);
httpRequest.send(null);
}
function readNewRecord(strdata){
var tempOut="";
var cateSet=strdata.getElementsByTagName("student"); //line43
for(var idx=0;idx<cateSet.length;idx++){
tempOut+="<dl><dt>"+cateSet[idx].getElementsByTagName("name")[0].childNodes[0].nodeValue+"</dt><dd>"+cateSet[idx].getElementsByTagName("sex")
[0].childNodes[0].nodeValue+"</dd></dl>";
}
document.getElementById("view").innerHTML=tempOut;
}
</script>
</head><body onload="sendRequest();">
<div id="view"></div>
</body>
</html>
我是纯用的JavaScript,没有Ajax技术,没有后台技术。
//==>
xmlDoc.async=false;
xmlDoc.load("b.xml");
按照这个方法,那个网页的问题确实解决了,不管是IE还是firefox都可以正常显示了,但问题又来了,我现在希望能把读取xml的方法写到一个单独的js文件里面去,在页面js里面只获取结点值。如果设置xmlDoc.async这个属性,不论是设成true还是设成false,都会导致在IE和firefox中都无法显示(IE还会运行出错),而如果不对这个属性进行设置,那么IE可以正常显示,firefox不能显示。请问这该怎么办呢?下面是单独的JS文件:JS文件名为common.jsfunction fReadXML()
{
var xmlDom=null;
if(window.ActiveXObject)
{
xmlDom=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('你的浏览器不支持XML');
return;
}
if(xmlDom!=null)
{
xmlDoc.async=true;
xmlDom.load("b.xml");
var person=xmlDom.getElementsByTagName("student");
return person;
}
}
下面是HTML文件:<html>
<head>
<script type="text/javascript" src="common.js"></script>
</head><body><script type="text/javascript">
var x=fReadXML();
document.write("<table border='1'>");
document.write("<thead>");
document.write("<tr><th>Artist</th><th>Title</th></tr>");
document.write("</thead>");
document.write("<tfoot>");
document.write("<tr><th colspan='2'>这是我学生</th></tr>");
document.write("</tfoot>"); for (var i=0;i<x.length;i++)
{
document.write("<tr>");
document.write("<td>");
document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
document.write("</td>"); document.write("<td>");
document.write(x[i].getElementsByTagName("sex")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");</script></body>
</html>
<head>
<script type="text/javascript" src="common.js"></script></head><body><script type="text/javascript">
//<script type="text/javascript" src="common.js"></script>中的fReadXML,覆盖过里面的内容
function fReadXML()
{
var xmlDom=null;
if(window.ActiveXObject)
{
xmlDom=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{
//xmlDoc=document.implementation.createDocument("","",null);
xmlDom=document.implementation.createDocument("","",null);
}
else
{
alert('你的浏览器不支持XML');
return;
}
if(xmlDom!=null)
{
// xmlDoc.async=true;
xmlDom.async=false;
xmlDom.load("b.xml");
var person=xmlDom.getElementsByTagName("student");
return person;
}
}
var x=fReadXML();
document.write("<table border='1'>");
document.write("<thead>");
document.write("<tr><th>Artist</th><th>Title</th></tr>");
document.write("</thead>");
document.write("<tfoot>");
document.write("<tr><th colspan='2'>这是我学生</th></tr>");
document.write("</tfoot>"); for (var i=0;i<x.length;i++)
{
document.write("<tr>");
document.write("<td>");
document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
document.write("</td>"); document.write("<td>");
document.write(x[i].getElementsByTagName("sex")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");</script></body>
</html>