Unable to access javascript function on <body> from <head>

R

ryan.mclean

Hello everyone. Hope ya'll had a nice New Year. Anyway, my question
is why won't this work? I must be doing something dumb . . . here is
the code:

in the body tag, I have this code (just to test):

<script LANGUAGE="javascript">
<!--
function hiThere() {
alert("hi");
return true;
}
//-->
</script>

in the head section, I have this code:

<script LANGUAGE="javascript">
<!--
document.body.onload=hiThere();
//-->
</script>

By the way, I know someone will comment, by "does not work, I mean on
the load of the page, the alert is not displayed, nor are any errors.
Thanks for any help you can offer.

Have a great day!
Ryan
 
M

Martin Honnen

in the body tag, I have this code (just to test):

<script LANGUAGE="javascript">
<!--
function hiThere() {
alert("hi");
return true;
}
//-->
</script>

in the head section, I have this code:

<script LANGUAGE="javascript">
<!--
document.body.onload=hiThere();
//-->
</script>

By the way, I know someone will comment, by "does not work, I mean on
the load of the page, the alert is not displayed, nor are any errors.

You should get errors, with script in the head
document.body
should be null so
document.body.onload
alone should trow an error.
And trying to call
hiThere()
in a script block before the script block that defines the function
hiThere should also cause an error.
If you want to use a function in a script block make sure it is defined
inside the same script block or in an earlier script block.

As for
document.body.onload=hiThere();
you probably want
document.body.onload=hiThere;
but for most browsers you need
window.onload = hiThere
anyway (of course that works only if the function is already defined).
 
R

ryan.mclean

Thanks for the reply Martin! Duh, I don't know what I was thinking.

What I am trying to do is render a javascript block in asp . . . but
that is a post for another group. I was thinking I could test what I
was trying to accomplish this way.

Anyway, thanks again!
Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top