multiple identical addEventListeners added multiple times?

11 views
Skip to first unread message

alta88[nntp]

unread,
Nov 29, 2012, 3:06:45 PM11/29/12
to

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

unread,
Nov 30, 2012, 5:06:28 AM11/30/12
to dev-te...@lists.mozilla.org
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
Reply all
Reply to author
Forward
0 new messages