...

View Full Version : xmlhttp.responseXML problem.



Element
01-20-2006, 05:20 AM
Attention Staff Members: I think I might have put this in the wrong forum now that I look at a brief summary of DOM...

I have this script on my site, and according to Velox Letum, it worked on his site, but for some reason it has a problem in IE and FireFox. Also, Velox said IE should be using Sarissa, why is it still trying to use the Ajax method?

Here is the line with the problem according to the error:


for (i = 0; i < xmlhttp.responseXML.getElementsByTagName('onlinenum')[0].firstChild.data; i++) {

And here is the whole script:


<script language='JavaScript' type='text/javascript'>
var xmlhttp = new XMLHttpRequest();
function reloadusers() {

// Set up the request
xmlhttp.open('POST', 'ajax.php', true);

// The callback function
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var users = new Array();
for (i = 0; i < xmlhttp.responseXML.getElementsByTagName('onlinenum')[0].firstChild.data; i++) {
users[i] = xmlhttp.responseXML.getElementsByTagName('userlink')[i].firstChild.data;
}
document.getElementById('users').innerHTML = '<b>Users Online</b><br />' + users.join(', ');
setTimeout("reloadusers()", 10000);
xmlhttp.close;
}
}
}

// Send the GET request
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlhttp.send('ajax=true');
}
reloadusers();
</script>

I know no JavaScript and am not sure whats wrong. the XML php file with the mysql results seems to be just fine.

Kor
01-20-2006, 12:25 PM
well, hm
mlhttp.responseXML.getElementsByTagName('onlinenum')[0].firstChild.data will return always a string, so that it can not be an iteration limit. Try

i < Number(xmlhttp.responseXML.getElementsByTagName('onlinenum')[0].firstChild.data)
or maybe

i < xmlhttp.responseXML.getElementsByTagName('onlinenum')[0].firstChild.data.length

...as I don't know exactly what kind of data lays in that textNode. Tell us what kind of loop should the code do, and what supposes to be the iteration limit.

liorean
01-20-2006, 05:54 PM
Also, it's a good idea to cache the limit in a variable instead of reevaluating it each time.
var
i=0,
limit=Number(xmlhttp.responseXML.getElementsByTagName('onlinenum')[0].firstChild.data),
coll=xmlhttp.responseXML.getElementsByTagName('userlink');
while(i<limit)
users[i]=coll.item(i++).firstChild.data;

Element
01-20-2006, 09:13 PM
:S I'll try what you guys said... hope i don't make it worse. Java and JavaScript are two languages i havn't successfully used. I mean, besides littlee snippets and such with no real point.

It still won't work. I added Kor's advise, both tries. I didn't add the while statement yete or the enew variabnles because eitheer way it should work and I can at that when it is working. Too see where it should be: http://chat.realmsrp.com/~dermetz/realmsrp.php

It should be below the user count and newest member in the last box on thee page. (Neveermind the bad coding job on th layout... I offered to code it, but he felt he needed a million little layout images. Lol.)

glenngv
01-23-2006, 09:29 AM
Try this:

var
i=0,
doc=xmlhttp.responseXML.documentElement,
limit=Number(doc.getElementsByTagName('onlinenum')[0].firstChild.data),
coll=doc.getElementsByTagName('userlink');
while(i<limit)
users[i]=coll.item(i++).firstChild.data;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum