Javascript not working in external file
<p>I'm trying some JS basics for a little test and have encountered with a problem when trying to put the Javascript code outside of the HTML one.</p>

<p>The HTML:</p>

<pre><code>&lt;body id="index" class="home"&gt;
&lt;table style="width:100%"&gt;
&lt;thead&gt;
&lt;tr&gt;
    &lt;th&gt;Region&lt;/th&gt;
    &lt;th&gt;Population 2012&lt;/th&gt;
    &lt;th&gt;Population 2035&lt;/th&gt;
    &lt;th&gt;Annual Growth&lt;/th&gt;
    &lt;th&gt;Daily Growth&lt;/th&gt;
    &lt;th&gt;Incr./Sec&lt;/th&gt;
    &lt;th&gt;Live Pop&lt;/th&gt;
    &lt;th&gt;GDP p/c annual&lt;/th&gt;
    &lt;th&gt;GDP p/c month&lt;/th&gt;
    &lt;th&gt;Incr./Sec&lt;/th&gt;
    &lt;th&gt;GDP 2012&lt;/th&gt;
    &lt;th&gt;GDP 2013&lt;/th&gt;
    &lt;th&gt;Area&lt;/th&gt;
    &lt;th&gt;Land&lt;/th&gt;
    &lt;th&gt;Water&lt;/th&gt;
    &lt;th&gt;Density&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tr&gt;
&lt;th&gt;EU&lt;/th&gt;
&lt;td&gt;503 492 041&lt;/td&gt;
&lt;td&gt;520 654 000&lt;/td&gt;
&lt;td&gt;746 172&lt;/td&gt;
&lt;td&gt;2 044&lt;/td&gt;
&lt;td&gt;0.02366&lt;/td&gt;
&lt;td&gt;&lt;label id="populationEU"&gt;503604514.99676&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;31 959.42 €&lt;/td&gt;
&lt;td&gt;2 663.29 €&lt;/td&gt;
&lt;td&gt;&lt;label id="GDPEUIncrement"&gt;0.00101&lt;/label&gt; €&lt;/td&gt;
&lt;td&gt;16 091 315 618 944.38 €&lt;/td&gt;
&lt;td&gt;&lt;label id="GDPEU"&gt;2 425 518 867 456.69&lt;/label&gt; €&lt;/td&gt;
&lt;td&gt;4 324 782.00 km&lt;sup&gt;2&lt;/sup&gt;&lt;/td&gt;
&lt;td&gt;4 191 578.71 km&lt;sup&gt;2&lt;/sup&gt;&lt;/td&gt;
&lt;td&gt;133 203.29 km&lt;sup&gt;2&lt;/sup&gt;&lt;/td&gt;
&lt;td&gt;116.42 hab/ km&lt;sup&gt;2&lt;/sup&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;h1 id="test"&gt;&lt;/h1&gt;
&lt;hr /&gt;
&lt;tr&gt;
&lt;th&gt;Asia&lt;/th&gt;
&lt;td&gt;4 227 067 000&lt;/td&gt;
&lt;td&gt;6 534 959 649&lt;/td&gt;
&lt;td&gt;84 541 340&lt;/td&gt;
&lt;td&gt;231 620&lt;/td&gt;
&lt;td&gt;&lt;label id="populationAsiaIncrement"&gt;2.6808&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;&lt;label id="populationAsia"&gt;4239810309.5035&lt;/label&gt;&lt;/td&gt;
&lt;td&gt;4 629.42 €&lt;/td&gt;
&lt;td&gt;385.79 €&lt;/td&gt;
&lt;td&gt;&lt;label id="GDPAsiaIncrement"&gt;0.00016&lt;/label&gt; €&lt;/td&gt;
&lt;td&gt;19 568 885 419 408.00 €&lt;/td&gt;
&lt;td&gt;&lt;label id="GDPAsia"&gt;3 120 792 273 016.10&lt;/label&gt; €&lt;/td&gt;
&lt;td&gt;44 579 000.00 km&lt;sup&gt;2&lt;/sup&gt;&lt;/td&gt;
&lt;td&gt;43 286 209.00 km&lt;sup&gt;2&lt;/sup&gt;&lt;/td&gt;
&lt;td&gt;1 292 791.00 km&lt;sup&gt;2&lt;/sup&gt;&lt;/td&gt;
&lt;td&gt;94.82 hab/ km&lt;sup&gt;2&lt;/sup&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;script type="text/javascript" src="js/counters.js"&gt;&lt;/script&gt;
&lt;/body&gt;
</code></pre>

<p>And the JS:</p>

<pre><code>var test = document.getElementById("test"); // Test placeholder

var millennium =new Date(2013, 0, 1); //Month is 0-11 in JavaScript
var today=new Date();
var one_day=60*60*24; //Get 1 day in seconds

//Calculate difference btw the two dates, and convert to days
var secondsSinceNewYear = Math.ceil(today.getTime()-millennium.getTime());

var populationEU = document.getElementById("populationEU");
var totalPopEU = 0;
var incrementEU = 1/2 ;

setInterval(popGrowthEU, 500);

function popGrowthEU()
{
totalPopEU = totalPopEU+incrementEU;
populationEU.innerHTML = addCommas(Math.round(totalPopEU*100)/100);
}

var GDPEU = document.getElementById("GDPEU");
var totalGDPEU = 0;
var incrementGDPEU = 1*totalPopEU/10;

setInterval(GDPGrowthEU, 100);

    function GDPGrowthEU()
    {
        totalGDPEU = totalGDPEU+incrementGDPEU;
        GDPEU.innerHTML = addCommas(Math.round(totalGDPEU*100)/100);
    }

var populationAsia = document.getElementById("populationAsia");
var incrementPopAsia = parseFloat(document.getElementById("populationAsiaIncrement").innerHTML);
var totalPopAsia = populationAsia.innerHTML+(incrementPopAsia*secondsSinceNewYear);
var incrementPopAsia = incrementPopAsia/2;
setInterval(popGrowthAsia, 500);

function popGrowthAsia()
    {
        totalPopAsia = totalPopAsia+incrementPopAsia;
        populationAsia.innerHTML = addCommas(Math.round(totalPopAsia*100)/100);
    }

var GDPAsia = document.getElementById("GDPAsia");
var incrementGDPAsia = document.getElementById("GDPAsiaIncrement").innerHTML;
var totalGDPAsia = incrementGDPAsia*secondsSinceNewYear*totalPopAsia;
var incrementGDPAsia = incrementGDPAsia*totalPopAsia/10;

setInterval(GDPGrowthAsia, 100);

function GDPGrowthAsia()
{
totalGDPAsia = totalGDPAsia+incrementGDPAsia;
GDPAsia.innerHTML = addCommas(Math.round(totalGDPAsia*100)/100);
}

function addCommas(nStr) {
nStr += '';
var x = nStr.split('.');
var x1 = x[0];
var x2 = x.length &gt; 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + ' ' + '$2');
}
return x1 + x2;
}
</code></pre>

<p><a href="http://jsfiddle.net/9GTFS/" rel="nofollow">http://jsfiddle.net/9GTFS/</a></p>

<p>I have some data, for now the initial data it's hardcoded, and  though i try to update certain fields (the ones with a "label" tag and id) in certain intervals using JS. When i do It putting the JS code in the HTML page, it works perfectly fine, but when I take it out and put it in a separate file it doesn't work. Actually, i managed to figure out that the line</p>

<p><code>var incrementPopAsia = parseFloat(document.getElementById("populationAsiaIncrement").innerHTML);</code></p>

<p>finds the problem that the label with that information (id 'populationAsiaIncrement') returns an "undefined" state and I'm stuck there, because I think everything's right. What could be causing this issue?
I think it's not a DOM issue because the JS is loaded at the very end of the page, so every bit of code has been loaded (and so all the DOM elements the javascript needs) and, in fact, the element that's failing (label id="populationAsiaIncrement") actually exists.</p>

<p>The specific problem that ends up showing is that <code>document.getElementById("populationAsiaIncrement").innerHTML</code> for some reason doesn't get assigned to the variable. If I execute just that in Firebug, it shows me the expected value, but when I try to assign it to a variable it gets "undefined".</p>
