Control order of onLoad events that fire?

Discussion in 'Javascript' started by Mike Cain, Oct 13, 2006.

  1. Mike Cain

    Mike Cain Guest

    Hi I have a JS library that I want others in my company to use that uses
    AttachEvent (etc) to add a method to the onLoad event so that it is called.
    Using this approach of course is good because then my onLoad event does not
    clobber that of others programmers in my company that may be already using
    onLoad for something else (in this case both their onLoad event and mine
    will fire).

    However I'm finding that in some cases it is important that my onLoad event
    fires prior to the other one. Or in some cases vice-versa.

    Is there any way when calling AttachEvent (or equivalents) to tell the
    browser to call your event last or first in the chain of onLoads?

    If not what would be the recommended solution to ensure my library can be
    included with other peoples JS libraries yet control itself to fire last (or
    in some cases first)?

    Thanks!
    Mike Cain, Oct 13, 2006
    #1
    1. Advertising

  2. Mike Cain

    Matt Kruse Guest

    Mike Cain wrote:
    > Is there any way when calling AttachEvent (or equivalents) to tell the
    > browser to call your event last or first in the chain of onLoads?


    Unfortunately, no.

    > If not what would be the recommended solution to ensure my library
    > can be included with other peoples JS libraries yet control itself to
    > fire last (or in some cases first)?


    If you all use the same library to add onload events, you can have a single
    onload event added. This event will keep its own internal list of events to
    fire, and you can control the stack of events by adding to the beginning or
    end. As long as everyone uses your interface for adding events, you can be
    assured that they will fire in a predictable order.

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
    Matt Kruse, Oct 13, 2006
    #2
    1. Advertising

  3. Mike Cain

    Mike Cain Guest

    Thanks Matt. The issue is that I cannot rely on other developers using my
    API to attach events for onLoad. In other words I need a solution that can
    dynamically move the events around to make sure it goes at a certain spot.
    Is this possible?

    For instance once in MY onLoad event is there a way I can see what other
    onLoad events are queued behind mine, and then move mine to the end of the
    list and call into the new event behind me - knowing that it'll bubble
    through all those and then eventually get back to my event since I moved it
    to last?

    Thanks.

    "Matt Kruse" <> wrote in message
    news:...
    > Mike Cain wrote:
    >> Is there any way when calling AttachEvent (or equivalents) to tell the
    >> browser to call your event last or first in the chain of onLoads?

    >
    > Unfortunately, no.
    >
    >> If not what would be the recommended solution to ensure my library
    >> can be included with other peoples JS libraries yet control itself to
    >> fire last (or in some cases first)?

    >
    > If you all use the same library to add onload events, you can have a
    > single onload event added. This event will keep its own internal list of
    > events to fire, and you can control the stack of events by adding to the
    > beginning or end. As long as everyone uses your interface for adding
    > events, you can be assured that they will fire in a predictable order.
    >
    > --
    > Matt Kruse
    > http://www.JavascriptToolbox.com
    > http://www.AjaxToolbox.com
    >
    >
    Mike Cain, Oct 17, 2006
    #3
  4. Mike Cain

    Matt Kruse Guest

    Mike Cain wrote:
    > In other words I need a
    > solution that can dynamically move the events around to make sure it
    > goes at a certain spot. Is this possible?


    Then I stick witrh my original answeer: Unfortunately, no.

    There is no way (that I know of) to enumerate the events attached to an
    object with addEventListener/attachEvent and/or manipulate them.
    If there *is* a way, I'd like to learn it :)

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
    Matt Kruse, Oct 17, 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:
    792
    Fabio R.
    Mar 25, 2005
  2. =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=

    order of events - dynamic checkbox events

    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=, Aug 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    561
    =?Utf-8?B?bG91aXNlIHJhaXNiZWNr?=
    Aug 17, 2005
  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. Replies:
    5
    Views:
    256
    Thomas 'PointedEars' Lahn
    May 15, 2005
  5. marco

    onload -->* no onload

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

Share This Page