onload without <body> tag?

Discussion in 'Javascript' started by bmgz, Jun 24, 2005.

  1. bmgz

    bmgz Guest

    I need to execute a JavaScript function "onload". The only problem is I
    don not have access to the <body> tag as it is a part of the standard
    page-header include (a separate file). How could I have certain pages
    execute my function() onLoad?

    The function basically just sets the original values of fields so that I
    can determine if a field has been changed or not, which aleviates unnec.
    sql update on the backend..
    bmgz, Jun 24, 2005
    #1
    1. Advertising

  2. bmgz wrote:

    > I need to execute a JavaScript function "onload". The only problem is I
    > don not have access to the <body> tag as it is a part of the standard
    > page-header include (a separate file). How could I have certain pages
    > execute my function() onLoad?


    Put
    window.onload = function (evt) {
    yourLoadHandler();
    };
    in a script (preferably included in the head section).

    In newer browsers you have also attach multiple event listeners e.g. for
    Mozilla or other DOM Level 2 Events compliant browsers you can do
    if (typeof window.addEventListener != 'undefined') {
    window.addEventListener(
    'load',
    function (evt) {
    yourLoadHandler();
    },
    false
    );
    }
    and in IE 5.5 and later you can do
    else if (typeof window.attachEvent != 'undefined') {
    window.attachEvent(
    'onload',
    function () {
    yourLoadHandler();
    }
    );
    }
    That way multiple independent scripts can have load handlers added.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jun 24, 2005
    #2
    1. Advertising

  3. bmgz

    RobG Guest

    bmgz wrote:
    > I need to execute a JavaScript function "onload". The only problem is I
    > don not have access to the <body> tag as it is a part of the standard
    > page-header include (a separate file). How could I have certain pages
    > execute my function() onLoad?
    >
    > The function basically just sets the original values of fields so that I
    > can determine if a field has been changed or not, which aleviates unnec.
    > sql update on the backend..


    function aFunction() {
    // do stuff
    }
    window.onload = aFunction;


    --
    Rob
    RobG, Jun 24, 2005
    #3
  4. bmgz

    Mick White Guest

    bmgz wrote:

    > I need to execute a JavaScript function "onload". The only problem is I
    > don not have access to the <body> tag as it is a part of the standard
    > page-header include (a separate file). How could I have certain pages
    > execute my function() onLoad?
    >
    > The function basically just sets the original values of fields so that I
    > can determine if a field has been changed or not, which aleviates unnec.
    > sql update on the backend..


    window.onload=function(){//do stuff here}

    But the body tag must not have an "onload" handler.
    Mick
    Mick White, Jun 24, 2005
    #4
  5. bmgz

    bmgz Guest

    RobG wrote:

    > function aFunction() {
    > // do stuff
    > }
    > window.onload = aFunction;


    Thanks, but I had already tried this before posting. I have all my
    JavaScript in a seperate .js file, could this be the problem?

    onload is triggering before the form has even loaded, because when I put
    the script after the form, it works.
    bmgz, Jun 24, 2005
    #5
  6. bmgz

    RobG Guest

    bmgz wrote:
    > RobG wrote:
    >
    >> function aFunction() {
    >> // do stuff
    >> }
    >> window.onload = aFunction;

    >
    >
    > Thanks, but I had already tried this before posting. I have all my
    > JavaScript in a seperate .js file, could this be the problem?
    >
    > onload is triggering before the form has even loaded, because when I put
    > the script after the form, it works.


    Do not include the brackets '()' after the function name:

    window.onload = aFunction(); // function will run immediately

    That will cause the function will run immediately rather than when the
    window has finished loading. If you need to pass parameters, use one
    of the other suggested methods, e.g.

    window.onload = function() {
    aFunction(parm1, parm2);
    };

    See the other posts.

    --
    Rob
    RobG, Jun 24, 2005
    #6
  7. bmgz

    bmgz Guest

    RobG wrote:
    > Do not include the brackets '()' after the function name:


    thanks for that.
    bmgz, Jun 24, 2005
    #7
  8. bmgz

    Lee Guest

    Tony said:

    >Wouldn't it work just to put
    >
    ><script type='text/javascript'>
    >// script to execute
    ></script>
    >
    >in the HTML body? My understanding is that any script placed in the body
    >will execute when the page is loaded.


    Actually, it's executed as the page is loading, which isn't quite the
    same thing.
    Lee, Jun 24, 2005
    #8
  9. bmgz

    Mick White Guest

    Tony wrote:

    > Martin Honnen wrote:

    [...]
    >>
    >>Put
    >> window.onload = function (evt) {
    >> yourLoadHandler();
    >> };
    >>in a script (preferably included in the head section).

    >
    >
    > It would appear from what he said that he can't put anything in the header,
    > either.
    >
    > Wouldn't it work just to put
    >
    > <script type='text/javascript'>
    > // script to execute
    > </script>


    <script type='text/javascript'>
    window.onload = function () {
    yourLoadHandler();
    }
    </script>

    You may place this anywhere on your page.
    Mick
    Mick White, Jun 24, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ~~~ .NET Ed ~~~
    Replies:
    5
    Views:
    1,355
    David Wainwright
    Mar 27, 2007
  2. Starry Gordon
    Replies:
    1
    Views:
    111
  3. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    543
    Martin Honnen
    Nov 4, 2004
  4. Dung Ping
    Replies:
    1
    Views:
    138
    Randy Webb
    Feb 27, 2005
  5. lwhitb1
    Replies:
    1
    Views:
    100
    Evertjan.
    Nov 22, 2005
Loading...

Share This Page