<script type="text/javascript">
    aLabe = document.getElementById('alabel') ;
    function init(){
         aLabe.innerHTML= "asdf" ;
    }
</script>
<body onload="init()">
<div id="container">
    <div id="header" > Google maps Intro 1</div>
    <div id="alabel">Yo!!</div>
    <div id="map_canvas"></div>
</div>
</body>

In the above,line 4 throws an aLabe is null error. Why is this? isn't aLabe a global var which is called before init()? If so then why doesn't init have access to aLabe ?

Member Avatar for stbuchok

What you are doing is, before the page is done loading, you are looking for a control called alabel. Because the page is not done loading (the control has not been rendered), it can not find the control. So now aLabe is null.

You either want to put line 3 inside the init function or put the script at the end of the page (just before the closing body tag).

Oh! so its not that the aLabe isn't available inside the function , its just that the var aLabe is null, because its being called before our div#label exists on the page. Thanks much for clearing this up for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.