multiple identical addEventListeners added multiple times?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

multiple identical addEventListeners added multiple times?

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.

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
_______________________________________________
dev-tech-dom mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-dom
Reply | Threaded
Open this post in threaded view
|

Re: multiple identical addEventListeners added multiple times?

Mounir Lamouri-3
On 29/11/12 20:06, alta88[nntp] wrote:
>
> 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
_______________________________________________
dev-tech-dom mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-dom