我有一台服务器,我正在尝试从FireFox中获取实时信息,并且在其他浏览器中,由于使用xml,实时数据不会填充。我理解chrome和什么不允许xml,但我不知道如何解决这个问题。想知道是否有人可以借助他们的知识来帮助我。这是代码。我认为问题在于否则函数loadXMLDoc基于chrome所说的错误--- xmldoc.load不是函数。
<SCRIPT language="JavaScript">
// Object to load the live standings
var xmldoc;
function loadXMLDoc(xmlurl) {
if (window.ActiveXObject) {
// code for IE
xmldoc = new ActiveXObject("Microsoft.XMLDOM");
xmldoc.onreadystatechange = state_Change
xmldoc.async = true
xmldoc.load(xmlurl);
} else if (document.implementation && document.implementation.createDocument) {
// code for Mozilla, Firefox, Opera, etc.
xmldoc = document.implementation.createDocument("","",null)
xmldoc.async = true
xmldoc.onload = populate
xmldoc.load(xmlurl)
} else {
// alert('Your browser cannot handle this script');
}
}
var interval, livetab
function populate() {
livetab = document.getElementById ? document.getElementById('livetable') : document.all.livetable
// First remove all existing rows from the body
for (var i = livetab.tBodies[0].rows.length - 1; i >= 0; i--) {
livetab.tBodies[0].deleteRow(i)
}
// Load the rows
if (xmldoc != null) {
var xn
// Do the common data here
xn = xmldoc.getElementsByTagName('track')[0].childNodes[0]
livetab.caption.innerHTML = (xn == null ? '' : xn.nodeValue)
xn = xmldoc.getElementsByTagName('server')[0].childNodes[0]
document.getElementById('server').innerHTML = (xn == null ? '' : xn.nodeValue)
xn = xmldoc.getElementsByTagName('session')[0].childNodes[0]
document.getElementById('session').innerHTML = (xn == null ? '' : xn.nodeValue)
xn = xmldoc.getElementsByTagName('progress')[0].childNodes[0]
document.getElementById('progress').innerHTML = (xn == null ? '' : xn.nodeValue)
for ( var row = 0; row < xmldoc.getElementsByTagName('standings').length; row++) {
// Add rows
var r = livetab.tBodies[0].appendChild(document.createElement('tr'))
r.className = 'detail' + (row & 1 ? 'odd' : 'even')
var c
c = document.createElement('td')
c.className = 'col1' + (row & 1 ? 'odd' : 'even')
xn = xmldoc.getElementsByTagName('position')[row].childNodes[0]
c.appendChild(document.createTextNode(xn == null ? '' : xn.nodeValue))
r.appendChild(c)
c = document.createElement('td')
c.className = 'col2' + (row & 1 ? 'odd' : 'even')
xn = xmldoc.getElementsByTagName('driver-name')[row].childNodes[0]
c.appendChild(document.createTextNode(xn == null ? '' : xn.nodeValue))
r.appendChild(c)
c = document.createElement('td')
c.className = 'col3' + (row & 1 ? 'odd' : 'even')
xn = xmldoc.getElementsByTagName('vehicle')[row].childNodes[0]
c.appendChild(document.createTextNode(xn == null ? '' : xn.nodeValue))
r.appendChild(c)
c = document.createElement('td')
c.className = 'col4' + (row & 1 ? 'odd' : 'even')
xn = xmldoc.getElementsByTagName('best-lap')[row].childNodes[0]
c.appendChild(document.createTextNode(xn == null ? '' : xn.nodeValue))
r.appendChild(c)
c = document.createElement('td')
c.className = 'col5' + (row & 1 ? 'odd' : 'even')
xn = xmldoc.getElementsByTagName('behind-leader')[row].childNodes[0]
c.appendChild(document.createTextNode(xn == null ? '' : xn.nodeValue))
r.appendChild(c)
c = document.createElement('td')
c.className = 'col6' + (row & 1 ? 'odd' : 'even')
xn = xmldoc.getElementsByTagName('last-lap-time')[row].childNodes[0]
c.appendChild(document.createTextNode(xn == null ? '' : xn.nodeValue))
r.appendChild(c)
c = document.createElement('td')
c.className = 'col7' + (row & 1 ? 'odd' : 'even')
xn = xmldoc.getElementsByTagName('laps-led')[row].childNodes[0]
c.appendChild(document.createTextNode(xn == null ? '' : xn.nodeValue))
r.appendChild(c)
c = document.createElement('td')
c.className = 'col8' + (row & 1 ? 'odd' : 'even')
xn = xmldoc.getElementsByTagName('total-laps')[row].childNodes[0]
c.appendChild(document.createTextNode(xn == null ? '' : xn.nodeValue))
r.appendChild(c)
c = document.createElement('td')
c.className = 'col12' + (row & 1 ? 'odd' : 'even')
xn = xmldoc.getElementsByTagName('finish-status')[row].childNodes[0]
c.appendChild(document.createTextNode(xn == null ? '' : xn.nodeValue))
r.appendChild(c)
}
}
// Refresh interval
clearInterval(interval)
interval = setInterval("loadXMLDoc('ARCALive?id=livedata')", 6000)
}
window.onload = function() { loadXMLDoc('ARCALive?id=livedata') }
//window.onload = function() { loadXMLDoc('ARCALive/ARCATest.xml') }
</script>
答案 0 :(得分:0)
您应该使用open()
不加载()
其次加载不是函数的原因是因为你没有做一个xml请求,因为chrome(和其他一些现代浏览器)中不存在ActiveXObject你必须使用XMLHttpRequest,而你使用document.implementation.createDocument没有得到xml文件,而只是创建一个空的html / xml文档
编辑:添加示例:
JS:
function getXML ( url ) {
var xmlReq = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLDOM');
xmlReq.onreadystatechange = function ()
{
if ( xmlReq.readyState == 4 && xmlReq.status == 200 )
{
createTable( xmlReq.responseXML );
}
};
xmlReq.open("GET", url, true);
xmlReq.send();
}
function createTable ( xmlDoc ) {
var table = document.createElement('table');
var tr;
var c;
//you should modify this to work with your xml
for (var i = 0; i < xmlDoc.childNodes[0].childNodes.length; i++) {
c = xmlDoc.childNodes[0].childNodes[i];
tr = document.createElement('tr');
for (var I = 0; I < c.childNodes.length; I++) {
tr.innerHTML += "<td>" + c.childNodes[I].innerHTML + "</td>";
}
table.appendChild(tr);
}
document.body.appendChild(table);
}
的test.xml:
<?xml version="1.0" encoding="UTF-8"?>
<tabel>
<row>
<test>a</test>
<test2>ab</test2>
</row>
<row>
<test>b</test>
<test2>bb</test2>
</row>
</tabel>
这样做:
getXML('test.xml');
会给你一张看起来像这样的表:
a ab
b bb