Control order of onLoad events that fire?

M

Mike Cain

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!
 
M

Matt Kruse

Mike said:
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.
 
M

Mike Cain

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.
 
M

Matt Kruse

Mike said:
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 :)
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top