Event Handling
With HTML 4.0, we were given the ability to tie scripts to certain events triggered on a web page. The most famous use of events is still probably JavaScript
rollovers on links, triggered by the onmouseover
and onmouseout
events. Of course, CSS can handle that much easier now, but it doesn’t render events useless, as there are many we can tap into (see Table 26-5) and put to good use.
Table 26-5. Common events
Event handler |
Event |
---|---|
|
An element loses focus (note: buggy). |
|
The content of a field changes. |
|
The mouse clicks an object. |
|
An error occurs when loading a document or an image. |
|
An element gets focus. |
|
A keyboard key is pressed. |
|
A keyboard key is pressed or held down. |
|
A keyboard key is released. |
|
A page or an image is finished loading. |
|
A mouse button is pressed. |
|
The mouse is moved. |
|
The mouse is moved off an element. |
|
The mouse is moved over an element. |
|
A mouse button is released. |
|
The submit button is clicked on a form. |
Tapping into events is not all that difficult to do. That said, there are good ways and bad ways. The old way to tap into an event was to place it inline as an attribute of the element you wanted the event handled on:
<a href="http://www.oreilly.com" onmouseover="window.status='Go to the O\'Reilly website'; return true;" onmouseout="window.status=''; return ...
Get Web Design in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.