I get problems with the "wrong setted 'this'" discussed here:
http://developer.mozilla.org/en/docs/DOM:element.addEventListener#Memory_issues
In my case, I have something like this:
var obj = {
func: function(event) {
event.target.removeEventListener("click", function(event) {
obj.func(event); }, true);
alert("test");
}
}
XXX.addEventListener("click", function(event) { obj.func(event); },
true);
Goal: Have the event listener in my object context, also get it called
in this context, but catch up only one click (so I want to unregister
the anonymous function after the first call).
... but for some reason the removeEventListener doesn't do anything...
Does someone know how to successfully remove an event listener on an
anonymous function?
Thanks in advance
CU
Manuel
Please note that specifying `true` above registers the listener for the
capture phase and Mozilla products implement the capture phase contrary
to the DOM Level 2 Events Recommendation. As for your issue, you can use
arguments.callee
to refer to the anonymous function, otherwise you have to store a refer-
ence to the handler, or make an object with a handleEvent "method" like
var obj = { handleEvent: function(evt) {
evt.currentTarget.removeEventListener('click', this, false);
} };
document.addEventListener('click', obj, false);
Here `this` is bound to `obj`, and evt.currentTarget is always bound to
the EventTarget the current listener is registered on.
--
Björn Höhrmann · mailto:bjo...@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
What's the meaning of this?
> As for your issue, you can use arguments.callee
> to refer to the anonymous function,
arguments.callee is good start. Thanks for the hint. But this references
to the called function itself. In case of an anonymous function like
this:
function(e){obj.func(e);}
a call to "arguments.callee" in context of "obj.func" will give me a
reference to "obj.func" and not the anonymous function.
But the following will do it:
arguments.callee.caller
It will return the calling function which is the anonymous function.
> But otherwise you have to store a refer-
> ence to the handler, or make an object with a handleEvent "method" like
> var obj = { handleEvent: function(evt) {
> evt.currentTarget.removeEventListener('click', this, false);
> } };
> document.addEventListener('click', obj, false);
But I need several event handlers in my object and so I would have to
have several objects in my object.
CU
Manuel