Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safe, universal, way to use addEventHandler in Javascript?

Before I get into the details of this problem, I'd like to make the situation clear. Our web analytics company works as a consultant for large sites, and (other than adding a single SCRIPT tag) we have no control over the pages themselves.

Our existing script installs handlers using "old" way (a fancy version of element.onclick = blah; that also executes the original handler) which is completely unaware of "new" (addEventListener or attachEvent) handlers on the page. We'd like to fix this to make our script able to run on more sites without requiring as much custom development.

The initial thought here was to have our own script use addEventListener/attachEvent, but this presents a problem: of the client's site sets a handler using the "old" way, it would wipe out the handler we installed the "new" way. Quick and dirty testing shows this happens in both IE7 and FF3, although I didn't test the whole range of browsers. There's also a risk that if we use the "new" way after the page's event handlers are already set, we could erase their handlers.

So my question is: what safe technique can I use to add an event handler in Javascript using addEventListener/attachEvent that works regardless of how other event handlers on the page are installed?

Please remember: we have no way of modifying the site that our script is installed on. (I have to emphasize that because the default answer to questions like this is always, "just rewrite the page to do everything the same way.")

like image 946
blakeyrat Avatar asked Oct 21 '08 18:10

blakeyrat


People also ask

What are alternative methods to add event listeners?

An alternative is to register an event listener at the root of the DOM tree ( document ). Then wait for events to bubble up and check whether the triggering element ( event. target ) matches the selector before you run your callback. This technique is called event delegation.

Can you add multiple event listener in JavaScript?

Can you have more than one event listener? We can add multiple event listeners for different events on the same element. One will not replace or overwrite another. In the example above we add two extra events to the 'button' element, mouseover and mouseout.

What is the use of addEventListener in JavaScript?

The addEventListener() is an inbuilt function in JavaScript which takes the event to listen for, and a second argument to be called whenever the described event gets fired. Any number of event handlers can be added to a single element without overwriting existing event handlers. Syntax: element.


2 Answers

Can you try your quick-and-dirty testing again? This doesn't happen for me in FF3.

elem.onclick = function() { alert("foo"); };
elem.addEventListener("click", function() { alert("bar"); }, false);

Both handlers fire for me when I click on the element.

I'm guessing you forgot the final boolean argument in addEventListener (whether to use the capture phase). I'm also guessing you forgot that IE's attachEvent needs onclick, not click.

like image 67
savetheclocktower Avatar answered Sep 30 '22 16:09

savetheclocktower


addEventListener/attachEvent is safe in a sense you ask. They add a new event handler to a Node without altering any handlers previously added to it (even once assigned through a property onxxx). For a company that bring some to a foreign page using addEventListener/attachEvent must be the only practice. Assigning onxxx handler via properties indeed would break hosting pages scipts (that have been previously assigned the same way)

like image 22
Sergey Ilinsky Avatar answered Sep 30 '22 16:09

Sergey Ilinsky