The HTML 4.0 specification includes what they call intrinsic events: a whole bunch of built-in hooks that the document writer/programmer can use to bind event handlers to particular events. These take the form of attributes which can be added to certain HTML tags.
<a href="#DummyLink" onmouseover="alert('You just handled the onmouseover event.')"> Place Mouse Here</a>
Above is a single line of HTML code contains a definition of the intrinsic event onmouseover. The value assigned to the intrinsic event is a script which will be called when the event occurs.
Let's try it out. Go ahead and move your mouse cursor across the following text:
You should have seen an alert window pop up.
The example is very short, but before we go on there are a number of important things to notice about it.
Let's try handling most of these events. The example below consists of three text areas and some buttons. In the top text area we've defined handlers for every event except onload and onunload (which are only used with the body or frame elements). In the lower left text area, each of the handlers except the one for onmousemove will print the name of their event each time they are called. The lower right text area is used by the onmousemove handler to write a "+" each time it is called. The set of checkboxes at the bottom will record which of the possible events have been triggered.
Go ahead and see how many event handlers you can trigger.
Don't be too disappointed if you find that you cannot trigger all the event handlers, especially if you're using NN4. We'll discuss the results in a bit, but first lets see the code:
For readability, the code shown above does not include the extra HTML used for layout, or most of the checkboxes.
Comparing this example to our first one-line event handler, aside from the fact that it is larger and uses forms, there are only a few additional ideas.
In the example we defined most of the event handlers as attributes of the TEXTAREA element that we are using for input. Referring to the Netscape HTML documentation for the TEXTAREA element we read that it supports exactly four of the event attributes:
This solves at least part of the mystery for NN4. Not all of it, however, because I still cannot get onselect to work, and I can get keydown, keyup and keypress.
With IE 5, I was able to trigger the handlers for all of the events except onkeyup, onkeydown. The Microsoft TEXTAREA documentation shows a long list of events that can be handled, and the list does include onkeyup and onkeydown. So once again we're left with a small mystery.
My other mystery was the absence of TEXTAREA onkeyup and onkeydown events in IE5. All I get is the onkeypress event. I had the same problem with an INPUT text element and an A element. However I finally discovered that the problem was not in IE itself. In my case IE was running as part my HTML editing application. The editor was trapping the keydown and keyup events itself. When I ran IE standalone it worked fine.
No more mysteries!
- W3C HTML 4.0 Specification
- See Section 18.2.3 on intrinsic events.
- The tutorial from Dr. Clue is the basis for the first part of this example.
- Refer to the sections on Events, the form and formName properties of the document object as well as the properties of the Form and TextArea classes.
- IRT article Man-Handling Events #1 by Ryan Detert
- IRT has some great articles on event handling written by Ryan Detert. Unfortunately, he seems to have misunderstood some things about events in Netscape.
- See the section on Events
- This is a private Netscape newsgroup that you will not find on other news servers. For information on Netscape newsgroups click here.