getElementsByTagName returns null value (but works weirdly fine in debugging mode)
<p>This code is taking in an xml file and parse it and output as a table. Here's a piece of code(Firefox oriented):</p>

<pre><code>function viewXML(inputForm) {
        var URL=inputForm.URL.value;    //input XML URL

        // create document and load URL     
        var xmlDoc = document.implementation.createDocument("","doc",null);
        xmlDoc.load(URL);

        xmlDoc.onload = generateHTML(xmlDoc);
        hWin = window.open("","sth", "height=800,width=600");
        hWin.document.write(html_text);
        hWin.document.close();
    }
</code></pre>

<p>viewXML is an onClick function of a button. Now a piece of code in <code>generateHTML()</code>:        </p>

<pre><code>function generateHTML(xmlDoc) {
    ...
    caption=xmlDoc.getElementsByTagName("title").item(0).firstChild.nodeValue;  //&lt;1&gt;
    html_text += "&lt;caption align='left'&gt;&lt;h1&gt;" + caption + "&lt;/h1&gt;&lt;/caption&gt;";
    ...
}
</code></pre>

<p>A piece of XML file:</p>

<pre><code>&lt;catalog&gt;
    &lt;title&gt;Airbus_Aircraft_Families&lt;/title&gt;
    &lt;aircraft&gt;
        &lt;Airbus&gt;A380&lt;/Airbus&gt;
        &lt;Aircraft&gt;A380&lt;/Aircraft&gt;
        &lt;seats&gt;555&lt;/seats&gt;
        &lt;Range&gt;15000km&lt;/Range&gt;
        &lt;Wingspan&gt;78.8m&lt;/Wingspan&gt;
        &lt;Height&gt;24.1m&lt;/Height&gt;
    &lt;/aircraft&gt;
&lt;/catalog&gt;
</code></pre>

<p><strong>Problem:</strong>
This code works fine when I was running it in debug mode step-by-step-ly in Firebug. BUT if there's no breakpoints set up, when the program hits line &lt;1>, there will show an error saying that:
<strong>xmlDoc.getElementsByTagName("title").item(0) is null</strong> , which is apparently not true.</p>

<p>And <strong>interestingly</strong>, when I tried adding a line</p>

<pre><code>alert(xmlDoc.getElementsByTagName("title").length)
</code></pre>

<p>before the &lt;1> line, it will pop up a window shows 0, but after I click "OK", the CORRECT title(from XML) will show up in the new window! As that in step-by-step mode.</p>

<p><strong>Things I tried:</strong></p>

<p>1, changing <code>xmlDoc.getElementsByTagName("title").item(0)</code> to <code>xmlDoc.getElementsByTagName("title")[0]</code></p>

<p>Didn't work, debugger told me "xmlDoc.getElementsByTagName("title")[0] is undefined"</p>

<p>2, Removed all the whitespaces within or between tags in XML file, didn't help.</p>
