#37 open
Bishop Bettini

Observed events fire in different orders

Reported by Bishop Bettini | December 2nd, 2008 @ 09:49 PM

According to the documentation, Event#observe "adds to the list of handlers" for a given element. From this, one might assume the events will fire in FIFO order or, minimally, fire in a consistent order across all browsers. Neither assumption is correct.

In my tests, IE fires in roughly LIFO order while all others fire in FIFO order. The only oddity I've found is that IE6 fires in arbitrary order for Prototype v.1.5.1.2. Here is a simple test case:

In try1.html:

<html>
<body>
<script type="text/javascript" src="common/code/Prototype.js"></script>
<script type="text/javascript" src="common/code/External.js"></script>
<script type="text/javascript">

alert('B');
Event.observe(window, 'load', function() {
    alert('2');
});

</script>
</body>
</html>

And in External.js:

alert('A');
Event.observe(window, 'load', function() {
    alert('1');
});

In most browsers, these alerts fire as: A, B, 1, 2. But in IE (in all released versions of Prototype 1.6), these fire as: A, B, 2, 1. Note that this behavior doesn't change when using document.observe('dom:loaded'), even after applying the patch from #127 to v1.6.0.3.

In the short term, the documentation should state, at a minimum, that the execution order is undefined. It would be nice to make claims for particular <browser-version,prototype-version> pairs.

In the long term, Prototype should smooth this difference out, ensuring a consistent firing order, ideally firing in FIFO order.

Comments and changes to this ticket

  • Bishop Bettini

    Bishop Bettini December 2nd, 2008 @ 09:51 PM

    Apparently, you need some formatting magic to show HTML. Let me try again (a preview button would be great!):

    In try1.html:

      <html>
      <body>
      <script type="text/javascript" src="common/code/Prototype.js"></script>
      <script type="text/javascript" src="common/code/External.js"></script>
      <script type="text/javascript">
      alert('B');
      Event.observe(window, 'load', function() {
           alert('2');
       });
      </script>
      </body>
      </html>
    
  • Bishop Bettini

    Bishop Bettini December 2nd, 2008 @ 10:10 PM

    Note that Event#observe uses this code to add an event listener:

          if (element.addEventListener) {
            element.addEventListener(name, wrapper, false);
          } else {
            element.attachEvent("on" + name, wrapper);
          }
    

    Regarding attachEvent(), from 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."

    Regarding addEventListener(), I didn't see any documentation about the firing order either at Mozilla (https://developer.mozilla.org/En/DOM/Element.addEventListener) or W3C (http://www.w3.org/TR/DOM-Level-2-Events/events.html)

    Based on how the code's written, it seems like Event#observe should use a dispatcher. The (element,event) pair gets a dispatcher, and the handler is pushed into the dispatcher. When the event is triggered, the dispatcher is called, and the dispatcher walks the registered handlers. This would ensure predicable fire order and seems reasonably consistent with the W3C recommendation.

    Thoughts?

  • Tobie Langel

    Tobie Langel December 2nd, 2008 @ 11:45 PM

    • State changed from “new” to “enhancement”

    AFAIK there is nothing in DOM specs mandating firing order to respect attaching order.

  • Andrew Dupont

    Andrew Dupont December 3rd, 2008 @ 12:37 AM

    • Tag set to events

    For a while, however, we've talked about doing this. Other frameworks have a single event listener method that manually executes the handlers in FIFO order. Sam had wanted to do something similar for his Event.stopImmediately function.

    So, yes, it is an enhancement, but one we'd get to anyway if we implemented any of several major event enhancements. I can't put a milestone on it, but let's keep this one around.

  • Bishop Bettini

    Bishop Bettini December 3rd, 2008 @ 04:11 PM

    In the interim, can we add a note to the documentation stating that event firing is not guaranteed to be the same as attaching order? That way, developers aren't assuming and then opening bug reports when actuality doesn't meet with expectation. :)

  • Andrew Dupont

    Andrew Dupont December 3rd, 2008 @ 04:28 PM

    • State changed from “enhancement” to “doc”
    • Assigned user set to “T.J. Crowder”

    Yeah, this is a doc bug in the meantime. Thanks!

  • Tobie Langel

    Tobie Langel May 12th, 2009 @ 04:02 PM

    • Milestone cleared.

    [responsible:ID#32948 milestone:ID#9627 bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:55 AM

    • Tag changed from events to section:dom

    [not-tagged:"events" tagged:"section:dom" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 03:56 AM

    [not-tagged:"events" tagged:"section:dom" bulk edit command]

  • T.J. Crowder

    T.J. Crowder November 16th, 2009 @ 04:50 PM

    • Assigned user cleared.

    [responsible:none bulk edit command]

  • Tobie Langel

    Tobie Langel November 29th, 2009 @ 07:18 PM

    • Milestone cleared.
    • Assigned user set to “Samuel Lebeau”
  • Tobie Langel

    Tobie Langel November 29th, 2009 @ 08:11 PM

    • State changed from “doc” to “open”
    • Tag cleared.
  • mindVex

    mindVex December 1st, 2010 @ 03:39 PM

    • Importance changed from “” to “”

    Is this going to be addressed sometime? I have the same problem. Two (custom) events attached to the same element. The second event is relying on a result the first event sets - this obviously doesn't work in IE as the order is vice-versa (random).

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Shared Ticket Bins

People watching this ticket

Pages