Element.fire event order

25 views
Skip to first unread message

vtsuper

unread,
Sep 22, 2008, 4:36:59 AM9/22/08
to Prototype & script.aculo.us
Dear all

I have try to use the element.fire to trigger 2 event

var FCK = Class.create({
initialize:function(id){
document.observe('FCK:completed',
this.func1.bindAsEventListener(this));
document.observe('FCK:completed',
this.func2.bindAsEventListener(this));
},
func1:function(){
alert('1');
},
func2:function(){
alert('2');
}
}

in FF the sequence is prompt '1' and then '2'
but in IE the sequence is prompt '2' and then '1'

and how can I control the sequence??

Regards
Victor

T.J. Crowder

unread,
Sep 22, 2008, 5:04:53 AM9/22/08
to Prototype & script.aculo.us
Hi,

As far as I'm aware the order in which event handlers are triggered is
implementation-dependent (and I don't think IE is quite as reliable as
it seems, it's not always last-in-first-fired). The only way you can
be sure of the order is to register only a single handler and have
that handler trigger other handlers in a known order (which is easy
enough). Event.observe doesn't do that, it relies on the underlying
event implementation of the browser.

A little Googling on, say, "addEventListener order" or "attachEvent
order" (both without the quotes) will bring you lots of info about
event order peculiarities.

FWIW,
--
T.J. Crowder
tj / crowder software / com

vtsuper

unread,
Sep 23, 2008, 12:04:42 AM9/23/08
to Prototype & script.aculo.us
thanks, you are correct, finally I find a solution from here
http://mootools.lighthouseapp.com/projects/2706/tickets/35-ie-s-attachevent-ff-s-addeventlistener-and-event-fire-order
> > Victor- Hide quoted text -
>
> - Show quoted text -

Justin Perkins

unread,
Sep 23, 2008, 12:52:56 AM9/23/08
to prototype-s...@googlegroups.com
Here's what I rely on to fire dom:loaded events in the order they were
attached to the document:

Event.register = function(object) {
// manage a stack of events to invoke
if (!Event.registeredEvents) Event.registeredEvents = $A();
if (!object['initialize']) return;

Event.registeredEvents.push(object);

// if the observer was already created, don't create another one
if (Event.domLoadedObserverCreated) return;
Event.observe(document, 'dom:loaded', function(){
// for each item in the stack, call the initialize method
Event.registeredEvents.each(function(object){
object.initialize();
});
});
Event.domLoadedObserverCreated = true;
};

// Example usage:
var Foo = {
initialize: function(){
// the dom:loaded event has fired
}
};
Event.register(Foo);

var Bar = Class.create({
initialize: function(){
}
};

Bar.initialize = function(){
};
Event.register(Bar);

Some might think that I'm crazy to maintain a custom stack of
dom:loaded events, but it works reliably in all major browsers, fires
events in the order you expect (in all browsers) and isn't susceptible
to event registration failures in IE when a large number of dom:loaded
listeners are created (something I ran into in the past).

Given an object, it will auto-invoke the initialize method. This code
primarily comes from Andrew Dupont, sans the custom event stack.
Advice/suggestions is welcome :)

-justin

ColinFine

unread,
Sep 23, 2008, 8:12:00 AM9/23/08
to Prototype & script.aculo.us


On Sep 22, 10:04 am, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> Hi,
>
> As far as I'm aware the order in which event handlers are triggered is
> implementation-dependent

"It is important to understand the the DOM standard makes no
guarantees about the order in which the handler functions of a single
object are invoked, so you should not rely on them being called in the
order in which you registered them." Flanagan, 'Javascript - The
Definitive Guide', 5th edition, p. 401

jdalton

unread,
Sep 23, 2008, 12:44:47 PM9/23/08
to Prototype & script.aculo.us
DOM Level 3 working draft says there should be a First in first out
order.
http://www.w3.org/TR/DOM-Level-3-Events/events.html#Events-flow

"This is the list of all event listeners that have been registered on
the current target in their order of registration."
"Finally, the implementation must process all candidate event
listeners in order and trigger each listener if all the following
conditions are met. "

- JDD

ColinFine

unread,
Sep 24, 2008, 7:15:03 AM9/24/08
to Prototype & script.aculo.us


On Sep 23, 5:44 pm, jdalton <John.David.Dal...@gmail.com> wrote:
> DOM Level 3 working draft says there should be a First in first out
> order.http://www.w3.org/TR/DOM-Level-3-Events/events.html#Events-flow
>
> "This is the list of all event listeners that have been registered on
> the current target in their order of registration."
> "Finally, the implementation must process all candidate event
> listeners in order and trigger each listener if all the following
> conditions are met. "
>
> - JDD

Hooray hooray hooray!!!

Of course certain standards-challenged browsers will continue going
their own sweet way, making this effectively useless ....

Colin

Reply all
Reply to author
Forward
0 new messages