E
Erwin Moller
Hi group,
I stumbled on something strange.
I simplified the problem to this:
A straightforward page with some JS:
<span id="testid">
Hi
</span>
<script type="text/javascript">
function alertID(){
var someID = document.getElementById("testid");
alert (someID);
}
// call it after loading
document.onload = alertID();
</script>
will produce an alert saying [object]
as expected.
But if I 'program' the function before the span, like this, it produces
null.
<script type="text/javascript">
function alertID(){
var someID = document.getElementById("testid");
alert (someID);
}
// call it after loading
document.onload = alertID();
</script>
<span id="testid">
Hi
</span>
That came as a surprise to me because I was thinking (and hoping) the
function would be evaluated AFTER the page loads.
It seems that JS is trying the function, then NOT finds the div (because it
is not on the page yet), and gives me null back, instead of evaluating the
function AFTER loading the page.
I have this behaviour on IE6 and Firefox.
Can anybody shed some light on this?
Regards,
Erwin Moller
I stumbled on something strange.
I simplified the problem to this:
A straightforward page with some JS:
<span id="testid">
Hi
</span>
<script type="text/javascript">
function alertID(){
var someID = document.getElementById("testid");
alert (someID);
}
// call it after loading
document.onload = alertID();
</script>
will produce an alert saying [object]
as expected.
But if I 'program' the function before the span, like this, it produces
null.
<script type="text/javascript">
function alertID(){
var someID = document.getElementById("testid");
alert (someID);
}
// call it after loading
document.onload = alertID();
</script>
<span id="testid">
Hi
</span>
That came as a surprise to me because I was thinking (and hoping) the
function would be evaluated AFTER the page loads.
It seems that JS is trying the function, then NOT finds the div (because it
is not on the page yet), and gives me null back, instead of evaluating the
function AFTER loading the page.
I have this behaviour on IE6 and Firefox.
Can anybody shed some light on this?
Regards,
Erwin Moller