Chinnery
Chinnery

Reputation: 10229

In Dojo or Javascript how do I make my event handler fire before other event handlers?

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?

Upvotes: 0

Views: 1437

Answers (4)

Alex
Alex

Reputation:

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

Upvotes: 0

user64640
user64640

Reputation: 93

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
}

Upvotes: 1

morgancodes
morgancodes

Reputation: 25265

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

javascript blur event -- is there any way to detect which element now has focus? ugly, but it works.

Upvotes: 0

Luca Matteis
Luca Matteis

Reputation: 29267

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.

Upvotes: 0

Related Questions