Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


Share this Page URL
Help

Chapter 25. W3C DOM Reference > EventListener - Pg. 785

Constants The following constant defines the one legal value for the code property of an EventException object. Note that this constant is a static property of EventException, not a property of indi- vidual exception objects. unsigned short UNSPECIFIED_EVENT_TYPE_ERR = 0 An Event object has a type property that is uninitialized, or is null or the empty string. Properties unsigned short code An error code that provides some detail about what caused the exception. In the Level 2 DOM there is only one possible value for this field, defined by the constant above. Description An EventException is thrown by certain event-related methods to signal a problem of some sort. (In the DOM Level 2 specification, an exception of this type is thrown only by EventTar get.dispatchEvent( ) ). EventListener Availability Methods handleEvent( ) an event handler function DOM Level 2 Events In languages such as Java that do not allow functions to be passed as arguments to other functions, you define an event listener by defining a class that implements this interface and includes an implementation for this handleEvent( ) method. When an event occurs, the system calls this method and passes in an Event object that describes the event. In JavaScript, however, you define an event handler simply by writing a function that accepts an Event object as its argument. The name of the function does not matter, and the function itself is used in place of an EventListener object. See the Section section. This interface defines the structure of an event listener or event handler. In languages such as Java, an event listener is an object that defines a method named handleEvent( ) that takes an Event object as its sole argument. In JavaScript, however, any function that expects a single Event argument, or a function that expects no argument, can serve as an event listener. Description Example // This function is an event listener for a "submit" event function submitHandler(e) { // Call a form-validation function defined elsewhere if (!validate(e.target)) e.preventDefault( ); // If validation fails, don't submit form } EventListener | 785