Discussion:
multiple identical addEventListeners added multiple times?
alta88[nntp]
2012-11-29 20:06:45 UTC
Permalink
Adding something like

x = document.getElementById("toolbar-menubar");
x.addEventListener("click", function(event) {
alert(event.button+":"+this.id); }, false);

multiple times (Fx17, using something like ExecuteJS), say executing it
4 times, will result in 4 alerts when the Fx menubar is clicked.

According to the doc[1] (and by longtime assumption), this shouldn't be
true and all but the first listener are discarded. Yet I get 4 alerts.
Seems very bad..

[1] https://developer.mozilla.org/en-US/docs/DOM/element.addEventListener
Mounir Lamouri
2012-11-30 10:06:28 UTC
Permalink
Post by alta88[nntp]
Adding something like
x = document.getElementById("toolbar-menubar");
x.addEventListener("click", function(event) {
alert(event.button+":"+this.id); }, false);
multiple times (Fx17, using something like ExecuteJS), say executing it
4 times, will result in 4 alerts when the Fx menubar is clicked.
You see this behaviour because you are using anonymous functions.
If you do this, it will work as expected:

function clickHandler(e, id) {
alert(e.button + ":" + id);
}
x = document.getElementById("toolbar-menubar");
x.addEventListener("click", clickHandler);

Note that the behaviour you are complaining about is the same in all
browsers I have been able to test.

Cheers,
--
Mounir

Loading...