Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 7 of 7

Thread: AJAX and childNodes[]

  1. #1

    AJAX and childNodes[]

    Hey, I've been trying to get the grips with some basic ajax, but i'm having some problems. I can't seem to get childNodes to ever work.

    [highlight=Javascript]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript" language="javascript">
    function makeRequest(url, e) {
    elem = document.getElementById(e);
    elem.innerHTML = "Loading...";
    if (window.XMLHttpRequest) {
    http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
    http_request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (!http_request) {
    alert('Giving up Cannot create an XMLHTTP instance');
    return false;
    }
    http_request.onreadystatechange = outContent;
    http_request.open('GET', url, true);
    http_request.send(null);
    }
    function outContent() {
    if (http_request.readyState == 4) {
    if (http_request.status == 200) {
    var xmldoc = http_request.responseXML;
    var root_node = xmldoc.getElementsByTagName('root');
    elem.innerHTML = root_node[0].childNodes[0].firstChild.text;
    } else {
    elem.innerHTML = 'There was a problem with the request.';
    }
    }
    }
    </script>
    </head>
    <body>
    <span style="cursor: pointer; text-decoration: underline" onclick="makeRequest('test.xml', 'load')">
    Make a request
    </span>
    <div id="load"></div>
    </body>
    </html>[/highlight]

    This is the code I'm using, and it's called this file:
    [highlight=XML]<?xml version="1.0" ?>
    <root>
    <text>I'm a test.</text>
    </root>[/highlight]

    If I run the hasChildNodes() function it returns true, and I can get the tagName variables of the root tag, but whenever I throw a childNodes into it all I see is "Loading..." with no output of the data.
    Build a man a fire and he is warm for a day.
    Set a man on fire and he is warm for the rest of his life.

  2. #2
    4,029
    posts
    home cooking is killing the restaurant industry
    It looks to me like you're encoutering the problem that the whitespace between the root node and the text node is a node itself..

    If the XML is represented as a tree, this is what it looks like:

    Code:
    root
         - #text (\n\t)
         - text
            - #text (I'm a test.)
         - #text (\n)
    Try childNodes[1] instead And just so you know, you wouldn't normally use innerHTML with AJAX.
    There are only 10 kinds of people in this world:
    Those that might know ternary, those that do, and those that don't
    Say NO to DRM.

  3. #3
    Quote Originally Posted by λ
    you wouldn't normally use innerHTML with AJAX.
    I'm still learning JS here, but would you use appendChild instead?
    Build a man a fire and he is warm for a day.
    Set a man on fire and he is warm for the rest of his life.

  4. #4
    176
    posts
    Registered User
    you could use appendChild but you're problem is still in referencing the opened XML ... I had this problem too, but I figured out it was browser specific. And by this I mean IE and everything else. I can't remember which did which but one reads that empty text node the other doesn't.

    I think this is right ... if not then ignore me
    PHP Code:
    <zip> echo this; </zip

  5. #5
    is there an
    Code:
    myXML .ignoreWhite
    in js as there is in flash?
    Build a man a fire and he is warm for a day.
    Set a man on fire and he is warm for the rest of his life.

  6. #6
    4,029
    posts
    home cooking is killing the restaurant industry
    No, there isn't.. Normally, you'd get around that by looping over childNodes and using nodeName to check whether the node is the one you want. And yes, you'd normally use the DOM functions, of which appendChild is one.
    There are only 10 kinds of people in this world:
    Those that might know ternary, those that do, and those that don't
    Say NO to DRM.

  7. #7
    176
    posts
    Registered User
    I found this page http://karaszewski.com/tools/ajaxlib/ which is a couple of JS functions that recursively strips white space from XML ...

    Seems pretty simple and straight forward
    PHP Code:
    <zip> echo this; </zip

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012