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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    xmlhttp.send(); break when html document gathers data from xml document

    I am attempting to use the exact W3C example at http://www.w3schools.com/xml/tryit.a..._display_table and when the page is rendered in both IE and Chrome it is blank. I believe the culprit is the 'xmlhttp.send()' line but cannot find related topics in this or other forums. Below is the complete html document.

    Code:
    <!DOCTYPE html>
    <html>
    <body>
    
    <script>
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("GET","cd_catalog.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML; 
    
    document.write("<table border='1'>");
    var x=xmlDoc.getElementsByTagName("CD");
    for (i=0;i<x.length;i++)
      { 
      document.write("<tr><td>");
      document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
      document.write("</td><td>");
      document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
      document.write("</td></tr>");
      }
    document.write("</table>");
    </script>
    
    </body>
    </html>
    I expected to be able to use the example right out of the box but to my surprise it does not work.

  • #2
    New Coder
    Join Date
    Aug 2013
    Posts
    50
    Thanks
    0
    Thanked 11 Times in 11 Posts
    Is cd_catalog.xml on the server that's running the JS code?

    If yes, the only thing I could think of is that you don't have an onload method, so maybe the request doesn't get competed before you try to display it? I'm not really that familiar with XMLHttpRequest, so spare me.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Get rid of the antiquated obsolete document.write call - such calls can only be run before the page finishes loading in the first place and are only required when your visitors are using Netscape 4. As Netscape 4 was also the last browser to NOT support XMLHttpRequest calls there has never been a time where combining those two statements in a single piece of code has worked.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    First step: As joesimmons said: *DOES* the file cd_catalog.xml exist on the SAME SERVER as that web page?

    If it does, then hit that URL directly and see what it gives you.

    For example, if you are testing this entirely on your own computer, and the URL to your test page is (example only)
    http://localhost/test.html
    then use the URL
    http://localhost/cd_catalog.xml
    to be sure that XML file is truly present.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,537
    Thanks
    77
    Thanked 4,381 Times in 4,346 Posts
    As for the rest of what Felgall said: Here's an example of a better (not best) way to do it:
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <table id="cdTable" border="1" cellpadding="5">
    <tr>
        <th>Artist</th>
        <th>Title</th>
    </tr>
    </table>
    
    <script type="text/javascript">
    xmlhttp = window.XMLHttpRequest != null
            ? new XMLHttpRequest()
            : new ActiveXObject("Microsoft.XMLHTTP");
    xmlhttp.open("GET","cd_catalog.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML; 
    
    var tbl = document.getElementById("cdTable");
    
    var x = xmlDoc.getElementsByTagName("CD");
    for (i=0; i<x.length; i++)
    {
        var artist = x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue;
        var title  = x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue;
        var row = tbl.insertRow(-1);
        var cell1 = row.insertCell(-1);
        cell1.innerHTML = artist;
        var cell2 = row.insertCell(-1);
        cell2.innerHTML = title;
    }
    </script>
    
    </body>
    </html>
    See? No obsolete and ugly document.write() calls.

    And, by the by, that code works.
    Last edited by Old Pedant; 08-12-2013 at 04:28 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,289
    Thanks
    12
    Thanked 345 Times in 341 Posts
    Quote Originally Posted by dsfcom View Post
    I am attempting to use the exact W3C example at http://www.w3schools.com/xml/tryit.a..._display_table
    important note: w3schools is in no way related to the W3C.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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