Must we use removeEventListener() for all addEventListener()implementation (in Firefox)?

Discussion in 'Javascript' started by sylver, Mar 19, 2008.

  1. sylver

    sylver Guest

    Hi,

    Unregistering event listeners (or memory allocation) is a good
    practice in programming, but do we need to use removeEventListener()
    for all addEventListener() implementation?

    Please consider this code:

    if(choice == a)
    button.addEventListener("click", function () {alert('just trying
    anonymous function over here');}, true);
    else
    button.addEventListener("click", function () {alert('here's where
    the else go');}, true);

    As you can see, there are 2 different functions (or maybe more) that
    may be attached to the button onclick event, depending on user's
    choice, and they are going to invoke anonymous functions.

    Questions:
    ===========
    1) Previous searches in the google groups suggested that there aren't
    a removeALLEventListener method? or is there?
    2) If i remove the button, using either removeChild() method or by
    closing the tab/window, will the event listener be removed as well?

    Please help.

    Thanks so much :D
     
    sylver, Mar 19, 2008
    #1
    1. Advertising

  2. sylver

    pr Guest

    sylver wrote:
    > Unregistering event listeners (or memory allocation) is a good
    > practice in programming, but do we need to use removeEventListener()
    > for all addEventListener() implementation?
    >
    > Please consider this code:
    >
    > if(choice == a)
    > button.addEventListener("click", function () {alert('just trying
    > anonymous function over here');}, true);
    > else
    > button.addEventListener("click", function () {alert('here's where
    > the else go');}, true);
    >
    > As you can see, there are 2 different functions (or maybe more) that
    > may be attached to the button onclick event, depending on user's
    > choice, and they are going to invoke anonymous functions.
    >
    > Questions:
    > ===========
    > 1) Previous searches in the google groups suggested that there aren't
    > a removeALLEventListener method? or is there?


    There isn't. You must call removeEventListener() with the same arguments
    you used in addEventListener().

    In simple cases, you can use arguments.callee to get a reference to the
    running (including anonymous) function, making this kind of once-only
    event listener possible:

    document.addEventListener("click", function (e) {alert("ouch!");
    document.removeEventListener("click", arguments.callee, false);
    }, false); // only reacts to the first click

    In more complex scenarios, you need to retain a reference to the
    function supplied when you registered the listener. See <URL:
    http://www.w3.org/TR/DOM-Level-2-Events>.

    One of the many ways your example might work, for instance, is thus:

    var listeners = [
    function (e) {alert("first");},
    function (e) {alert("second");}
    ];

    button.addEventListener("click", listeners[choice], true);
    ...
    button.removeEventListener("click", listeners[choice], true);


    > 2) If i remove the button, using either removeChild() method or by
    > closing the tab/window, will the event listener be removed as well?


    Hopefully yes; I don't know of any bugs or leaks - although I haven't
    seen an IE implementation yet :)
     
    pr, Mar 19, 2008
    #2
    1. Advertising

  3. sylver wrote:
    > Unregistering event listeners (or memory allocation) is a good
    > practice in programming, but do we need to use removeEventListener()
    > for all addEventListener() implementation?
    >
    > Please consider this code:
    >
    > if(choice == a)
    > button.addEventListener("click", function () {alert('just trying
    > anonymous function over here');}, true);
    > else
    > button.addEventListener("click", function () {alert('here's where
    > the else go');}, true);


    If an event listener is to be added anyway, why not consider the following:

    button.addEventListener(
    "click",
    (function(a, b) {
    if (a == b)
    {
    return function() {
    window.alert('just trying anonymous function over here');
    };
    }
    else
    {
    return function() {
    window.alert('here's where the else go');
    };
    }
    })(choice, a),
    true);

    BTW, is there a point to the event capturing (`true') or is it just the
    result of copy-and-pray?

    > As you can see, there are 2 different functions (or maybe more) that
    > may be attached to the button onclick event, depending on user's
    > choice, and they are going to invoke anonymous functions.
    >
    > Questions:
    > ===========
    > 1) Previous searches in the google groups suggested that there aren't
    > a removeALLEventListener method? or is there?


    Well, there is window.location.reload(). Seriously: no, unless you
    implement a registration of event listeners.

    > 2) If i remove the button, using either removeChild() method or by
    > closing the tab/window, will the event listener be removed as well?


    The event listener is a Function object. As with all objects, they are not
    marked for garbage collection before there are no more references to it.
    However, as with the event target a host object is involved, MSHTML may have
    problems cleaning up the mess; see the FAQ Notes on closures. On the other
    hand, IE up to version 7 does not support addEventListener() anyway.


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Mar 19, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. philjhanna
    Replies:
    1
    Views:
    117
    Martin Honnen
    Aug 9, 2005
  2. Replies:
    2
    Views:
    163
  3. Andrew Poulos
    Replies:
    1
    Views:
    95
    Bjoern Hoehrmann
    Sep 1, 2008
  4. William Wallace
    Replies:
    25
    Views:
    1,053
    David Mark
    Aug 14, 2010
  5. Integralist
    Replies:
    1
    Views:
    107
Loading...

Share This Page