SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy removeEventListener and anonymous function

    Hi folks,

    From my research so far, I don't think this is possible, but just wanted to post the question in here anyways because I did learn a lot by posting to this forum and who knows, hopefully someone has the answer.

    Now, the problem is that I am allowing the user to be able to add events to certain element and remove events from them. But, if the user adds an event with anonymous function, is there any way that removeEventListener method will be able to remove it. If not, is there any other way? If yes, could you please show an example?

    Really appreciate for your time and posting any comments. Thanks
    With Regards
    Pman
    http://www.pmansLab.com

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can remove all the eventListeners at once by cloning the element and replacing the original with the clone. Event handlers aren't cloned.

    You lose any benefit of using an anonymous function if you need to remove it later- it can't really be anonymous, can it? You need some way to refer to it to remove it.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Quote Originally Posted by mrhoo View Post
    You lose any benefit of using an anonymous function if you need to remove it later- it can't really be anonymous, can it? You need some way to refer to it to remove it.
    You are absolutely right. I didn't even think about it that way. Thanks for the cloning suggestion. I will try it out.

    But is there any way to check if an event is already registered with an element. Basically I want to have that checked so that the user doesn't add multiple click event.

    Thanks for your help.
    With Regards
    Pman
    http://www.pmansLab.com

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you can do this if you are using the traditional event registration model which adds event listeners to elements as properties. In the WC3 or the Microsoft models there is no way of testing what event listeners you have already assigned to an element, unless you keep this info stored somewhere like in an object literal on the element itself and check before you'd start adding another click event listener.

    function addListener(elem,func,eventType)
    {
    if(typeof elem["on"+eventType].toLowerCase()!="function")
    {
    elem["on"+eventType]=func;
    }
    /* un-comment the following to add multiple functions to the same event listener, or write an error message handling part here in its place
    else
    {
    var oldListener=elem["on" + eventType];
    elem["on"+eventType]=function(){ oldListener(); func();}
    }
    */
    }

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Third Stone From The Sun
    Posts
    82
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mrhoo View Post

    You lose any benefit of using an anonymous function if you need to remove it later- it can't really be anonymous, can it? You need some way to refer to it to remove it.
    1) There are no benefits to using anonymous functions, except for scope closing tricks, but that doesn't really apply in this case.
    2) You can remove an anon function that is assigned to a variable.
    This is an anoymous function. It has no name.
    Code:
    var anAnonFn = function() { };
    This function is named "notAnonymous"
    Code:
    function notAnonymous() { };
    This function is named "pork". Function pork is assigned to the var notAnonII.
    DO NOT DO THIS in practice. It is very confusing to have a function named differently from its variable assignment.
    Code:
    notAnonII = function pork() { };
    Last edited by DHTML Kitchen; Jun 1, 2007 at 11:35. Reason: detail notes.
    My outdated site is down for a while now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •