views:

248

answers:

3

We have a Web system that uses a combination of OnBlur and OnMouseDown events. Unfortunately, for a strange reason the OnMouseDown event handler is being triggered before calling the OnBlur event handler.

This is not what we want. Instead, we want to always call the OnBlur event handler prior to calling the onMouseDown event handler. Is there a way to do so, perhaps giving the onBlur a higher priority?

Previously, instead of using the onMouseDown event handler we had the onclick event. However, this posed a number of problems especially due to the single-threaded nature of JavaScript.

A: 

Catch event #2, fire event #1. Then let event #2 go through.

.. Catch-and-release pattern :)

roosteronacid
Thanks a lot. Can you kindly provide a simple example or a pointer for further info on this?
@roosteronacid, what if only one of the events happens? Do you still call the body method?
PanCrit
A: 

You'll have to fake it by using a status variable. It's a bit dirty, but it works: the meat of doImportantStuff will only be run once.

var importantStuffDone = false;
function doImportantStuff(){
     if(!importantStuffDone){
          // Do something important
          importantStuffDone = true;
     }
}

function doOnBlur(){
     // This function gets bound to the blur event
     doImportantStuff();
     // Do other blur stuff
}
function doOnMouseDown(){
     // This function gets bound to the mousedown event
     doImportantStuff();
     // Do other mousedown stuff
}
cpharmston
Thanks a lot. I got your solution but what we want is that the onBlur event is always called prior to calling the onmousedown event. So the gist of the question is more about changing Javascript Event handling priority or precedence.
You can't; and even if you could, legacy browsers' implementation of event systems are so different that it'd end up breaking in something else. The best you can do is fake it, like this.
cpharmston