Java Reference
In-Depth Information
Its first parameter is the type of event and the second is a callback function that is invoked
when the event occurs. In this example, we are using the click event and an anonymous
function that produces an alert dialog:
addEventListener("click", function(){
alert("You Clicked!")
});
Alternatively, a named function could be used:
function doSomething() {
alert("You Clicked!");
}
addEventListener("click",doSomething);
The parentheses are not placed after the function when it is used as the argument to an
event listener; otherwise, the function will actually be called when the event listener is set,
instead of when the event happens.
Note: Cross-browser Events
All modern browsers now support the standard event listeners. Unfortu-
nately, this has not always been the case, where older versions of Internet
Explorer used a different syntax. If you need to support these browsers,
JavaScript Rules has some suggestions for creating a cross-browser event
listener function.
Search WWH ::




Custom Search