onload handler order of firing

Discussion in 'Javascript' started by Adam Ratcliffe, Jan 23, 2006.

  1. I have 2 onload handlers on a web page. One is set programatically in
    an included script and the other declared on the document's body
    element.

    In practice the programatically set onload handler always fires first
    in Firefox (the desired behaviour).

    Do I have any guarantee that this ordering will be the same across
    other browsers?

    Cheers
    Adam
     
    Adam Ratcliffe, Jan 23, 2006
    #1
    1. Advertising

  2. Adam Ratcliffe

    RobG Guest

    Adam Ratcliffe wrote:
    > I have 2 onload handlers on a web page. One is set programatically in
    > an included script and the other declared on the document's body
    > element.
    >
    > In practice the programatically set onload handler always fires first
    > in Firefox (the desired behaviour).
    >
    > Do I have any guarantee that this ordering will be the same across
    > other browsers?


    That seems a rather risky dependency, even if it is 'works' in practice,
    that is easily proved to fail in at least one case (see below).

    If the script element is in the header, the browser should parse the
    content before getting to the body tag (the defer attribute can suggest
    that it doesn't, but I'll guess you're not using that). But I'm not
    sure that the onload events will be attached in the sequence you require
    with any certainty.

    A much safer method would be to have your own init() function that you
    can add functions to so that you can guarantee that they run in the
    right order.

    Consider the following simple example:

    <head>
    <title>onload test</title>
    <script type="text/javascript">

    function hiScript() {
    alert('hi, script 1');
    }

    if (window.addEventListener) {
    window.addEventListener("load", hiScript, false);
    } else {
    window.attachEvent('load',hiScript);
    }

    </script>

    </head>
    <body onload="alert('hi, body');">
    </body>


    The alerts appear in the right order in Firefox (script then body), but
    in IE I get only the onload attached in the body tag.


    --
    Rob
     
    RobG, Jan 23, 2006
    #2
    1. Advertising

  3. Hi Rob

    Your quite correct about there being no guarantee of the order in which
    the event handlers will be called. Yesterday I had the opportunity to
    test on a windows machine and the body element's onload handler was
    called first (the opposite to FF).

    The reason that the onload handler defined in the script tag wasn't
    invoked in your example was because attachEvent() requires that the
    event type is prefixed with 'on'.

    Cheers
    Adam
     
    Adam Ratcliffe, Jan 24, 2006
    #3
  4. Adam Ratcliffe

    VK Guest

    Adam Ratcliffe wrote:
    > I have 2 onload handlers on a web page. One is set programatically in
    > an included script and the other declared on the document's body
    > element.
    >
    > In practice the programatically set onload handler always fires first
    > in Firefox (the desired behaviour).
    >
    > Do I have any guarantee that this ordering will be the same across
    > other browsers?


    There were some test made in this group a couple of month ago. It was
    shown that addEventListener sequence is preserved, but attachEvent is
    randomized (MSDN actually clearly states that). Also these test have
    been made for addEventListener/attachEvent only (w/o intrinsic
    handlers) and for Firefox / IE only. So on the global run no - the
    sequence is not guaranteed. As suggested, if the sequence is crucial
    for your case, you have to keep your own pool manually.
    ....
    function init() {
    for (var i=0; i<init.stack.length; i++) {
    var foo = init.stack();
    }
    } init.stack = new Array();
    ....
    if (window.onload != null) {
    init.stack.push(window.onload);
    }
    window.onload = init;
    init.stack.push(extraFunction1);
    init.stack.push(extraFunction2);
    .... etc.
     
    VK, Jan 24, 2006
    #4
    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. Fabio R.
    Replies:
    0
    Views:
    803
    Fabio R.
    Mar 25, 2005
  2. =?Utf-8?B?THluZXJz?=

    onload not firing

    =?Utf-8?B?THluZXJz?=, Sep 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    534
    =?Utf-8?B?THluZXJz?=
    Sep 28, 2005
  3. David Otton

    window.onload and body.onload differences

    David Otton, Nov 4, 2004, in forum: Javascript
    Replies:
    2
    Views:
    559
    Martin Honnen
    Nov 4, 2004
  4. Replies:
    5
    Views:
    264
    Thomas 'PointedEars' Lahn
    May 15, 2005
  5. marco

    onload -->* no onload

    marco, Jun 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    207
    marco
    Jun 24, 2006
Loading...

Share This Page