Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between event handlers & listener?

What is the difference between event handlers and event listeners in JavaScript? They both execute a function when the event appears. I don't really get when to use event handlers and when to use event listeners.

like image 305
js-coder Avatar asked Aug 03 '11 16:08

js-coder


People also ask

What is the difference between event and event handler?

The event is the thing that RAISES an event, to which something will subscribe. The EventHandler is the thing that HANDLES an event - i.e. it specifies the method that is used to subscribe to the event.

What is a event handler?

In programming, an event handler is a callback routine that operates asynchronously once an event takes place. It dictates the action that follows the event. The programmer writes a code for this action to take place. An event is an action that takes place when a user interacts with a program.

What's the difference between event handlers & Addeventlistener in JS?

There's no difference; it's just different terminology for the same thing. There are different ways of associating functions with DOM elements for the purpose of event handling, that's all.


2 Answers

A handler and a listener are one in the same - just synonyms for the function that will handle an event. "Handler" is probably the more accepted term, and is certainly more semantically correct to me. The term "listener" is derived from the code used to add an event to an element:

element.addEventListener('click', function() { /* do stuff here*/ }, false); 

You could, however, get really nitpicky and break the two down into separate meanings. If you're so inclined, "handler" could be the term for the function that is going to handle an event when you add a "listener", thus one can have several "listeners" that utilize a single "handler". Consider:

// handler is synonymous with function  function someFunction(e) {   if (typeof e == 'undefined')    alert('called as a function');   else    alert('called as a handler'); }   // use someFunction as a handler for a  // click event on element1 -- add a "listener" element.addEventListener('click', someFunction, false); // use an anonymous function as a handler for a  // click event on element1 -- add another "listener" element.addEventListener('click', function () { alert('anonymoose'); }, false);   // use someFunction as a handler for a  // click event on element2 -- add a "listener" element2.addEventListener('click', someFunction, false);  // call someFunction right now someFunction(); 

So in the above code, I have 2 "handlers" (someFunction and an anonymous function) and 3 "listeners".

Again, this is all semantics - for all practical purposes the terms listener and handler are used interchangeably. If a distinction need be made then a listener is a subscription to an event that will trigger a call to a handler (which is a function).

Clear as mud?

like image 123
Chris Baker Avatar answered Oct 12 '22 09:10

Chris Baker


There's no difference; it's just different terminology for the same thing.

There are different ways of associating functions with DOM elements for the purpose of event handling, that's all. The differences emerged back when standards were in flux (or just because implementors were ornery or difficult) but ultimately the mechanisms are essentially the same.

If you're confused about what sort of event handler registration to use, you can:

  • Read more about the topic and choose an approach to use, perhaps on a browser-by-browser basis;
  • Choose one of the popular JavaScript frameworks and use its mechanism for attaching handlers
like image 36
Pointy Avatar answered Oct 12 '22 09:10

Pointy