Javascript: Controlling the order that event handlers / listeners are exeucted in

Posted by LRE on Stack Overflow See other posts from Stack Overflow or by LRE
Published on 2010-03-23T21:36:19Z Indexed on 2010/03/23 21:43 UTC
Read the original article Hit count: 410

Once again the IE Monster has hit me with an odd problem.

I'm writing some changes into an asp.net site I inherited a while back. One of the problems is that in some pages there are several controls that add javascript functions as handlers to the onload event (using YUI if that matters). Some of those event handlers assume certain other functions have been executed.

This is well and good in Firefox and IE7 as the handlers seem to execute in order of registration. IE8 on the other hand does this backwards.

I could go with some kind of double-checking approach but given the controls are present in several pages I feel that'd create even more dependencies. So I've started cooking up my own queue class that I push the functions to and can control their execution order. Then I'll register an onload handler that instructs the queue to execute in my preferred order.

I'm part way through that and have started wondering 2 things:

  1. Am I going OTT?
  2. Am I reinventing the wheel?

Anyone have any insights? Any clean solutions that allow me to easily enforce execution order?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about events