that makes sense thanks :) But I'm building my own js library that abstracts the api between browsers and obviously there is the chance that a user with will specify an anonymous function as the handler. How does a library like jQuery's bind() method handle these situations ?
Sent from my iPhone On 23 Mar 2011, at 13:36, Poetro <[email protected]> wrote: > 2011/3/22 Mark McDonnell <[email protected]>: >> I'm having a bizarre issue removing an event listener (this occurs in both >> Firefox and Chrome). >> >> If I run the following code via the console… >> >> document.getElementsByTagName('body')[0].addEventListener('click', >> function(e){ alert(e); }, false); >> >> …the event listener is added successfully, but if I run the following code >> also in the console to remove the event listener it doesn't appear to >> actually remove the event listener?... >> >> document.getElementsByTagName('body')[0].removeEventListener('click', >> function(e){ alert(e); }, false); >> >> …I'm really confused why this wouldn't work? > > Those listeners are totally different functions. If you are planning > to remove an event listener, make it a separate function, and use that > with addEventListener and removeEventListener. For example: > > function clickHandler(e) { > alert(e); > } > document.getElementsByTagName('body')[0].addEventListener('click', > clickHandler, false); > // ... > document.getElementsByTagName('body')[0].removeEventListener('click', > clickHandler, false); > > Creating anonymous functions each time creates 2 different functions, > so you cannot remove it, as u haven't added that, but a different > function, that has the same implementation. Just think of: > > var a = {}, b = {}; > alert(a === b); // false > > -- > Poetro > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected] -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
