Loading ...
Sorry, an error occurred while loading the content.

event execution order difference for Event.addListener between IE & Firefox

Expand Messages
  • Chris
    I ran into a problem when trying to use Event.addListener in 2 different ways. The problem is the execution order is different for IE and Firefox. Here s the
    Message 1 of 2 , Apr 7, 2006
    • 0 Attachment
      I ran into a problem when trying to use Event.addListener in 2
      different ways. The problem is the execution order is different for
      IE and Firefox. Here's the detail:

      Method1:
      YAHOO.util.Event.addListener ( document.myform.mybutton, "click",
      myfunc1);

      Method2 (:
      window.onload = myfunc;
      function myfunc () {
      // a bunch of other initialization calls
      YAHOO.util.Event.addListener ( document.myform.mybutton, "click",
      myfunc2);
      }


      Because of the complexity of the js usage, I have an external JS file
      registering the click method using the method in "Method2". Then, on
      the need of different pages, Method1 is added. The thing is, on IE,
      the order is Method2 -> Method1, but on Firefox, it's Method1 ->
      Method1. This causes some problem for my code. I went through the
      Yahoo code, and saw that it's got code to handle this, but I'm not
      sure why it still executes differently.

      I could do something to work around this. But I'm wondering if this
      is due to the different implementation of browsers? In my opinion, I
      think that onload should always finish before any other events. So I
      kind of agree with the IE order.

      Any comments?




      Here's my sample code:


      test.html:

      <html>
      <head><title>test</title>
      </head>
      <body>

      <script type="text/javascript" language="JavaScript"
      src="YAHOO.js"></script>
      <script type="text/javascript" language="JavaScript"
      src="event.js"></script>
      <script type="text/javascript" language="JavaScript"
      src="test.js"></script>

      <form name="abc" method=get>
      <input type="button" name="mybut" value="press me"
      onclick="alert('used onClick')">
      </form>

      <script>

      YAHOO.util.Event.addListener ( document.abc.mybut, "click", function () {
      alert ('document.abc.mybut.attachEvent=func');
      } );

      </script>

      </body>
      </html>







      test.js:




      window.onload = function () {

      YAHOO.util.Event.addListener( document.abc.mybut, "click", function()
      { alert('onload, YAHOO.util.Event.addListener'); } );

      };
    • Adam Moore
      ... If you register multiple events of the same type on the same element, the order the listeners may not be the same from one browser to the next. Most
      Message 2 of 2 , Apr 10, 2006
      • 0 Attachment
        On Fri, Apr 07, 2006 at 09:31:24AM -0000, Chris wrote:
        > I ran into a problem when trying to use Event.addListener in 2
        > different ways. The problem is the execution order is different for
        > IE and Firefox. Here's the detail:

        If you register multiple events of the same type on the same element, the
        order the listeners may not be the same from one browser to the next. Most
        browsers execute the listeners in the order they were registered, but
        IE does not.

        CustomEvent does execute its listeners in order. If you need to guarantee
        the order, you can create a custom event to wrap the DOM event.


        -Adam
      Your message has been successfully submitted and would be delivered to recipients shortly.