The current popular opinion is that everything must be unobtrusive, which means that event handler properties such as someElement.onclick = function(e) { ... };
are widely frowned upon and event handler attributes such as <input type="button" onclick="doSomething()">
are outright dismissed. In fact, there are valid uses for both.
The desire to separate behaviour (such as scripted event handlers) from content is a natural one and is a major consideration, but should not be the only consideration. As shown in the summary below, the three methods of creating event handlers have their own advantages and disadvantages, and in a particular situation, separation of behaviour and content may not be the overriding concern.
In conclusion, for a given task, use the simplest method that fits your needs.
Event handler properties
Example
myElement.onclick = function(e) { alert("Clicked"); };
These are particularly useful for assigning an event handler to an element that you create in script and are certain will only ever require one listener.
Advantages
- Separates behaviour from content
- Works in all scriptable browsers
- Works the same in all browsers, apart from the issue of where the Event object comes from (
window.event
in IE, function parameter in other browsers)
- Universally supported method of preventing the default browser behaviour using
return false
Disadvantages
- Allows only one listener for a particular object and event
- On elements in the HTML source, handlers are typically not assigned until document is loaded
Event handler attributes
Example
<input type="button" value="test" onclick="alert('Clicked');">
This is the only approach that works when it matters that an element responds to an event before the document has finished loading (see
http://peter.michaux.ca/articles/the-window-onload-problem-still for a longer discussion of this). Also, it's the easiest way to add event handlers.
Advantages
- Works in all scriptable browsers
- Works the same in all browsers
- Works as soon as the element is rendered
- Simplest way to add an event handler
- Universally supported method of preventing the default browser behaviour using
return false
- On a very simple page, it's the most readable method
- Standardized in the HTML 4 spec
Disadvantages
- Mixes behaviour with content
- Allows only one listener for a particular object and event
addEventListener/attachEvent
Example (attachEvent equivalent not shown)
myElement.addEventListener("click", function(e) { alert("Clicked"); }, false);
This is the only method that allows you to attach multiple listeners to a particular event on a particular object. addEventListener
is standardized in the DOM Level 2 Events specification.
Advantages
- Separates behaviour from content
- Allows multiple event listeners
addEventListener
is a modern standard, with support in IE 9 meaning that all current major browsers will support on release of IE 9
Disadvantages
- Slightly complicated to implement correctly cross-browser
- IE's
attachEvent
is not exactly equivalent to addEventListener
- On elements in the HTML source, handlers are typically not assigned until document is loaded