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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ryan.mclean@gmail.com

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

    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="javas cript">
    <!--
    function hiThere() {
    alert("hi");
    return true;
    }
    //-->
    </script>

    in the head section, I have this code:

    <script LANGUAGE="javas cript">
    <!--
    document.body.o nload=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

  • Martin Honnen

    #2
    Re: Unable to access javascript function on &lt;body&gt; from &lt;head&gt;



    ryan.mclean@gma il.com wrote:

    [color=blue]
    > in the body tag, I have this code (just to test):
    >
    > <script LANGUAGE="javas cript">
    > <!--
    > function hiThere() {
    > alert("hi");
    > return true;
    > }
    > //-->
    > </script>
    >
    > in the head section, I have this code:
    >
    > <script LANGUAGE="javas cript">
    > <!--
    > document.body.o nload=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.[/color]

    You should get errors, with script in the head
    document.body
    should be null so
    document.body.o nload
    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.o nload=hiThere() ;
    you probably want
    document.body.o nload=hiThere;
    but for most browsers you need
    window.onload = hiThere
    anyway (of course that works only if the function is already defined).
    --

    Martin Honnen

    Comment

    • ryan.mclean@gmail.com

      #3
      Re: Unable to access javascript function on &lt;body&gt; from &lt;head&gt;

      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

      Comment

      Working...