Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Javascript for dummies 4th edition - Veer E.V

Veer E.V Javascript for dummies 4th edition - Wiley publishing , 2004. - 387 p.
ISBN: 0-7645-7659-3
Download (direct link): javascriptfordummies2005.pdf
Previous << 1 .. 76 77 78 79 80 81 < 82 > 83 84 85 86 87 88 .. 118 >> Next

You see examples of event handlers throughout this book. For this chapter, I focus on the event handlers that most developers find most useful — the event handlers associated with window, mouse, form, and keyboard events. Here you find out how to create and attach essential scripts to such user-driven events as opening a Web page window, moving a mouse pointer, interacting with an HTML form, and pressing keys.
The Skinny on Events and Event Handlers
In Web programming terms, an event is anything that happens to a Web page. Examples of events include
1^ A window or frame opening or closing
A mouse pointer moving onto or off of an image, a link, or an HTML form A mouse clicking or double-clicking anywhere on a page A key being pressed
240 Part IV: Interacting with Users
By using JavaScript, you can perform an action in response to any event. For example, you can play a welcoming tune when a user opens a Web page; display explanatory text when a user mouses over a certain area of a Web page; validate form data as soon as a user enters it; and create hot keys that perform a custom function in response to a keystroke.
Handling Events
You handle events in JavaScript by attaching event handlers to individual Web page elements. Event handlers are simply attribute-value pairs that allow you to capture and respond to events as they occur.
For example, the following code defines an HTML button that, when clicked, calls a custom JavaScript function. In this sample code, the name of the event handler is onClick, and the name of the JavaScript function is calc().
<INPUT TYPE="button"
ME="pushButton" VALUE="Calculate Total" onClick=''calc() ">
Table 13-1 lists the events (in addition to click) that you can handle in both Navigator and Internet Explorer by using JavaScript — along with the Web page elements, or objects, that support those events.
Netscape Navigator and Internet Explorer provide different levels of support for event handlers, as they do for so many other features of JavaScript and HTML and HTML extensions and . . . well, you get the picture. In addition, because event handling is inherently platform-dependent, browsers implemented on Macintosh and Unix systems offer differing levels of support for events. For additional event handling information, point your browser to
http://msdn.microsoft.com/workshop/author/dhtml/reference/events.asp

Table 13-1 Common Cross-Browser Web Page Elements (Objects) That Support Event Handlers
Event Handler Supporting Objects Event (Event Handler Triggered When...)
onAbort Image The image loading is interrupted.
onBlur Button, Checkbox, FileUpload, Password, Radio, Reset Select, Submit, Text, Textarea,window (frame) The element loses input focus. (Clicking out of or tabbing away from an element takes away that element's input focus.)
Chapter 13: Handling User-Initiated Events 241
Event Handler Supporting Objects Event (Event Handler Triggered When...)
onChange Checkbox, FileUpload, Password, Radio, Select, Text, Textarea The element changes (for example, the user types text into a Text element or clicks a Radio button) and loses input focus.
onClick Button,Checkbox, document, FileUpload, Image, Link, Password, Radio, Reset, Select, Submit, Text, Textarea The element is clicked a single time (combination of onMouseDown and onMouseUp).
onDblClick Button,Checkbox, document, FileUpload, Image, Link, sion Password, Radio, Reset, Select, Submit, Text, Textarea The element is clicked twice in quick succes- (double-clicked).
onError Image The image doesn't finish loading for some reason. (Perhaps the image file doesn't exist or is corrupted.)
onFocus Button, Checkbox, FileUpload, Frame, Password, Radio, Reset, Select, Submit, Text, Textarea,window (frame) The element gains input focus.
onKeyDown Button,Checkbox, document, FileUpload, Image, Link, Password, Radio, Reset, Select, Submit, Text, Textarea The user presses a key.
onKeyPress Button,Checkbox, document, FileUpload, Image, Link, Password, Radio, Reset, Select, Submit, Text, Textarea onKeyUp event handlers). The user presses and releases a key (which combines the onKeyDown and
onKeyUp Button,Checkbox, document, FileUpload, Image, Link, Password, Radio, Reset, Select, Submit, Text, Textarea The user releases a previously pressed key.
onLoad Image, window (frame) The element loads successfully.
(continued)
242 Part IV: Interacting with Users
Table 13-1 (continued)
Event Handler Supporting Objects Event (Event Handler Triggered When... )
onMouseDown Button, Checkbox, document, FileUpload, Image (and Area), Link, Password, Radio, Reset, Select, Submit, Text, Textarea The user presses a mouse button (but doesn't release it).
onMouseOut Image (and Area), Link The mouse moves off the element.
onMouseOver Image (and Area), Link The mouse moves onto the element.
onMouseUp Button,Checkbox, document, FileUpload, Image (and Area), Link, Password, Radio, Reset, Select, Submit, Text, Textarea The user releases a previously clicked mouse button.
Previous << 1 .. 76 77 78 79 80 81 < 82 > 83 84 85 86 87 88 .. 118 >> Next