views:

634

answers:

4

In the Dojo Javascript library, I understand how to use dojo.connect or dojo.publish to wire up my event handler to an event. That functionality works great.

But I want to do one additional thing.

I want my event handler to fire first, before any other already defined event handlers. I suppose this could be called "event handler insertion" or something.

Is there a way to do that in Dojo or a convenient and elegant way in plain Javascript?

A: 

When events are fired they are connected to their action (onload, onclick). So how would you fire an event before other actions are triggered?

For example, how would you fire an event before an onclick if you don't know when it's going to be triggered ?

This is definitely something you can't do, maybe you want to register handlers to the onload event which is usually triggered before all the others.

Luca Matteis
A: 

I had a (possibly) related problem, and wound up using setTimeout to set a flag.

http://stackoverflow.com/questions/528208/javascript-blur-event-is-there-any-way-to-detect-which-element-now-has-focus ugly, but it works.

morgancodes
A: 

Try modifying your event like this:

function alertTest(){
 alert('test');
}

function alertTest2(){
 alert('test2');
}

window.onload = alertTest2; //just setting it to simulate that an event
       //is already set

oldOnload = window.onload; //store the old event into a temporary variable
window.onload = function() { //assign a new function to the event
 alertTest(); //whatever your "must happen first" function is
 oldOnload.apply(); //run the original event after
}
A: 

As far as I know, once the event handlers are attached to the DOM node, the sequence of handlers execution is implementation dependent.