Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts

    xmlhttp.responseXML problem.

    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:

    Code:
    for (i = 0; i < xmlhttp.responseXML.getElementsByTagName('onlinenum')[0].firstChild.data; i++) {
    And here is the whole script:

    Code:
    <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.
    Last edited by Element; 01-20-2006 at 04:29 AM.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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.
    Last edited by Kor; 01-20-2006 at 04:58 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Also, it's a good idea to cache the limit in a variable instead of reevaluating it each time.
    Code:
    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;
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    Regular Coder Element's Avatar
    Join Date
    Jul 2004
    Location
    Lynnwood, Washington, US
    Posts
    855
    Thanks
    2
    Thanked 2 Times in 2 Posts
    :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.

    Edit: 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.)
    Last edited by Element; 01-20-2006 at 08:21 PM.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Try this:
    Code:
    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;
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •