4.8. Event HandlingBring up the subject of client-side events among a group of web developers, and the first (sometimes the only) one mentioned is the onclick event handler. Occasionally, someone will acknowledge the onmouseover and the onmouseout events, but that is usually a rare occurrence, such as leap year or a pay raise after Y2K. Come to think of it, you're more likely to hear a story about someone holding a door open for Walter Koenig than to hear the smallest utterance about another event. The problem is that developers get into a rut, a comfort zone, and use the same events day in and day out. After a few months of this, we have a tendency to forget that the event handlers are even there. One of the reasons for this is that developing web applications is like riding a bike; when you don't remember how to do it right, there isn't even time to scream before the splat. For this reason, I have compiled Table 4-5, which covers the event handlers common to most browsers. Yes, Bill, that means that the beforeunload event is omitted.
Unfortunately, knowing the events is only half the battle. For this knowledge to be of any use, it is necessary to know how to assign a JavaScript event to the handler. And as with many endeavors in JavaScript, there are two ways to accomplish this task. No, I'm not referring to a right way and a wrong way; I'm referring to assigning via HTML and via JavaScript. Listing 4-13 shows both ways to assign an event handler. Listing 4-13. The Two Ways to Assign an Event Handler in JavaScript
Before wrapping up this chapter, there are some important items that could fall under the umbrella of event handling. Although they aren't really events, they do raise events. The items that I am referring to are the window.setTimeout() and window.setInterval() methods. Don't be surprised if you've never heard of them; they're a little "out there." The purpose of these methods is to delay the execution of a JavaScript function for a specific number of milliseconds. Why? Well, let's say, for example, that you'd like to check later to see if an event has taken place and leave it at that. The real question is really, why are there two methods instead of one? The reason for two methods is that setTimeout executes a function once, whereas setInterval executes a function repeatedly until told otherwise. Think of setInterval as being afflicted with lycanthropy, and you get the concept. The syntax, shown here, for both of these methods is identical: var oTime = window.setTimeout('myFunction()',1000); var oInterval = window.setInterval('myYour()',100); All that is left is what to do when it is necessary to clear a timeout or an interval. It is simple; just do the following, and they're cleared: window.clearTimeout(oTime); window.clearInterval(oInterval); Remember one important thing when coding in JavaScript: Bending the rules is allowed. Experiment, and delve into matters that man, or woman, was not meant to delve into. After all, it is the mad scientist way. |