If you use attachEvent
for IE you cannot guarantee the order in which events are fired. See http://msdn.microsoft.com/en-us/library/ms536343(VS.85).aspx
If you attach multiple functions to
the same event on the same object, the
functions are called in random order,
immediately after the object's event
handler is called.
(I actually have seen that events are called in reverse order, not random). Anyway, libraries generally tend to use attachEvent
deep down, so you're stuck with the same problem.
Having said that, if you can inspect a node for it's 'click' handlers (eg you have an "onclick" attribute setup on your node in your markup), then you can put "yours" ahead of "theirs":
var nodes = document.getElementsByTagName('*'); // collect your nodes however
for(var i=0; i < nodes.length; i++) {
var node = nodes[i];
if(!node.onclick)
continue;
// At this point we have a node with an "onclick" attr.
// Hijack onclick to do something else first.
var original = node.onclick;
node.onclick = function() {
doSomethingElse();
return original.apply(this, arguments);
}
}
See this answer for how to inspect the a node's events in other libraries (if you are using one). You may be able to use it to doSomethingElse()
with those libraries too.