Sams Teach Yourself JavaScript in 24 Hours
-
Table of Contents
- Copyright
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Part I: Getting Started
- Hour 1. Understanding JavaScript
- Hour 2. Creating a Simple Script
- Hour 3. How JavaScript Programs Work
- Part II: Learning JavaScript Basics
- Hour 4. Using Functions and Variables
- Hour 5. Using Strings and Arrays
- Hour 6. Testing and Comparing Values
- Hour 7. Repeating Yourself: Using Loops
- Hour 8. Using Math and Date Functions
- Part III: The Document Object Model (DOM)
- Hour 9. Working with the Document Object Model
- Hour 10. Responding to Events
- Hour 11. Using Windows and Frames
- Hour 12. Getting Data with Forms
- Hour 13. Using Graphics and Animation
- Part IV: Moving on to Advanced JavaScript Features
- Hour 14. Creating Cross-Browser Scripts
- Hour 15. Creating Custom Objects
- Hour 16. Working with Sounds and Plug-Ins
- Hour 17. Debugging JavaScript Applications
- Part V: Working with Dynamic HTML (DHTML)
- Hour 18. Working with Style Sheets
- Hour 19. Using Dynamic HTML (DHTML)
- Hour 20. Using Advanced <em>DOM</em> Features
- Part VI: Putting It All Together
- Hour 21. Improving a Web Page with JavaScript
- Hour 22. Creating a JavaScript Game
- Hour 23. Creating <em>DHTML</em> Applications
- Hour 24. JavaScript Tips and Tricks
- Part VII: Appendices
- Appendix A. Other JavaScript Resources
- Appendix B. Tools for JavaScript Developers
- Appendix C. Glossary
- Appendix D. JavaScript Quick Reference
- Appendix E. DOM Quick Reference
Using Mouse Events
JavaScript includes a number of event handlers for detecting mouse actions. Your script can detect the movement of the mouse pointer and when a button is clicked, released, or both.
Over and Out
You've already seen the first and most common event handler, onMouseOver. This handler is called when the mouse pointer moves over a link or other object.
The onMouseOut handler is the opposite—it is called when the mouse pointer moves out of the object's border. Unless something strange happens, this always happens some time after the onMouseOver event is called.
This handler is particularly useful if your script has made a change when the pointer moved over the object—for example, displaying a message in the status line or changing an image. You can use an onMouseOut handler to undo the action when the pointer moves away.
You'll use both onMouseOver and onMouseOut handlers in the "Workshop: Adding Link Descriptions to a Web Page" section at the end of this hour.
Using the onMouseMove event
The onMouseMove event occurs any time the mouse pointer moves. As you might imagine, this happens quite often—the event can trigger hundreds of times as the mouse pointer moves across a page.
Because of the large number of generated events, browsers don't support the onMouseMove event by default. To enable it for a page, you need to use event capturing. This is similar to the dynamic events technique you learned earlier in this hour, but requires an extra step for Netscape browsers.
The basic syntax to support this event, for both browsers, is to set a function as the onMouseMove handler for the document or another object. For example, this statement sets the onMouseMove handler for the document to a function called MoveHere, which must be defined in the same page:
document.onMouseMove=MoveHere;
Additionally, Netscape requires that you specifically enable the event using the document.captureEvents method:
document.captureEvents(Event.MOUSEMOVE);
Ups and Downs
You can also use events to detect when the mouse button is clicked. The basic event handler for this is onClick. This event handler is called when the mouse button is clicked while positioned over the appropriate object.
For example, you can use the following event handler to display an alert when a link is clicked:
<a href="http://www.jsworkshop.com/"
onClick="alert('You are about to leave this site.')">Click Here</a>
In this case, the onClick event handler runs before the linked page is loaded into the browser. This is useful for making links conditional or displaying a disclaimer before launching the linked page.
If your onClick event handler returns the false value, the link will not be followed. For example, the following is a link that displays a confirmation dialog. If you click Cancel, the link is not followed; if you click OK, the new page is loaded:
<a href="http://www.jsworkshop.com/"
onClick="return(window.confirm('Are you sure?'))">
Click Here</a>
This example uses the return statement to enclose the event handler. This ensures that the false value that is returned when the user clicks Cancel is returned from the event handler, which prevents the link from being followed.
The onDblClick event handler is similar, but is only used if the user double-clicks on an object. Because links usually require only a single click, you could use this to make a link do two different things depending on the number of clicks. (Needless to say, this could be confusing.) You can also detect double-clicks on images and other objects.
To give you even more control of what happens when the mouse button is pressed, two more events are included:
- onMouseDown is used when the user presses the mouse button.
- onMouseUp is used when the user releases the mouse button.
These two events are the two halves of a mouse click. If you want to detect an entire click, use onClick. Use onMouseUp and onMouseDown to detect just one or the other.
To detect which mouse button is pressed, you can use the which property of the event object. This property is assigned the value 1 for the left button, or 3 for the right button. This property is assigned for onClick, onDblClick, onMouseUp, and onMouseDown events.
For example, this script creates an onMouseDown event handler that displays an alert indicating which button was pressed.
function mousealert(e) {
whichone = (e.which == 1) ? "Left" : "Right";
message = "You clicked the " + whichone + " button.";
alert(message);
}
document.onmousedown = mousealert;
Using Keyboard Events | Next Section