1

My script is loading some data from an XML file and printing a table with it.

    function draw_schedule() {
    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","schedule.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;

    document.write("<table width='100%' border='1'>");
    var x=xmlDoc.getElementsByTagName("day");
    for (i=0;i<x.length;i++) {// number of days
      document.write("<tr><th colspan='2'>");
      document.write(x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);// the date for each day
      document.write("</th></tr>");
      var y=x[i].getElementsByTagName("session");// daily sessions
      for (j=0;j<y.length;j++) {
            document.write("<tr><td>");
            document.write(x[i].getElementsByTagName("title")[j].childNodes[0].nodeValue);
            document.write("</td><td>");
            document.write(x[i].getElementsByTagName("time")[j].childNodes[0].nodeValue);
            document.write("</td></tr>");
    }
  }
    document.write("</table>");
}

If I call the function (separate file) from the HTML file, it prints the table and afterward prints 'undefined'. If I alternatively embed the script in the HTML, it prints the table without printing 'undefined'. I can't figure-out why having the script in a separate file would change its behaviour. I would love for someone with more wisdom than I to explain. Thanks!

1
  • Are you completely sure that using document.write() is what you want to do? For a lot of reasons that's a pretty undesirable way to go about building up page content. Commented Dec 4, 2010 at 22:10

2 Answers 2

1

draw_schedule() returns no value (undefined). You're possibly calling the function with document.write:

document.write(draw_schedule());

draw_schedule() returns undefined in this case, and the result would look like this:

document.write(undefined);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's helpful. I really don't know why I was calling the function with document.write. Sometimes these things just need another set of fresh eyes!
If your question is answered, please mark the answer as accepted. At the moment, there are 206,491 questions with no accepted answers. That's a pretty huge list :/
1

Do your document.write inside this:

xmlhttp.open("GET", "schedule.xml", false);
xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4) {
    if(xmlhttp.status == 200) {
    var result = xmlhttp.responseXML;
            // do your document.write
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.