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

Event.removeListener and "unload" events

Expand Messages
  • michal_frackowiak
    Hi, yesterday I was trying to unregister an event listener attached by a command: YAHOO.util.Event.addListener(body, unload , somefunction); where body is the
    Message 1 of 1 , Mar 28, 2006
    • 0 Attachment
      Hi,

      yesterday I was trying to unregister an event listener attached by a
      command:

      YAHOO.util.Event.addListener(body, "unload", somefunction);

      where body is the body element.

      Under some circumstances however I wanted to remove this listener:

      YAHOO.util.Event.removeListener(body, "unload", somefunction);

      which returns false. The reason is that the unload listeners are
      treated in a different way and stored in an array unloadListeners[]
      and processed differently when the page unloads.

      The problem is that the removeListener() method only checks if the
      given binding is present in the listeners[] array. When not found the
      method returns false.

      The soultion is to check valid bindings not only in the listeners[]
      array but also in the unloadListeners[]. The original removeListener()
      has a code block:

      if (!el || !cacheItem) {
      return false;
      }

      which makes the method return false in either the element is not valid
      or a binding does not exists.

      It would be nice to add a unloadListeners[] at this point:


      if (!el || !cacheItem) {
      // perhaps this is an "unload" event? check this!
      for (var i=0; i< unloadListeners.length; ++i) {
      var li = unloadListeners[i];
      if ( li &&
      li[this.FN] == fn &&
      li[this.EL] == el &&
      li[this.TYPE] == sType ) {
      index = i;
      }
      }
      if (index >= 0) {
      delete unloadListeners[index];
      return true;
      }
      return false;
      }

      which works fine for me. Perhaps it would be nice to include such a
      mdification to the package.

      best regards
      Michal Frackowiak

      BTW: awesome package. many thanx to all the developers and contributors!!!
    Your message has been successfully submitted and would be delivered to recipients shortly.