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
- Using Image Maps with JavaScript
- Using Dynamic Images in JavaScript
- Creating Rollovers
- Workshop: Creating a Simple Animation
- Summary
- Q&A
- Quiz
- Exercises
- 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 DOM Features
- Part VI: Putting It All Together
- Hour 21. Improving a Web Page with JavaScript
- Hour 22. Creating a JavaScript Game
- Hour 23. Creating DHTML 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 Image Maps with JavaScript
Image maps are a popular way to provide navigation for a site. They're images that are divided into a number of "hot spots," or areas that act as links. Using JavaScript, you can perform script commands when an area of the image is clicked.
As an example of using a client-side image map with JavaScript, let's create a simple image map menu for a fictional company. (The company's name isn't important, but it's obviously not a graphic design company.)
To create a client-side map, you need to do three things:
- Use your favorite graphics application to create the actual graphic as a GIF or JPEG image.
- Create a MAP definition that describes areas in the image.
- Include the image map in the document, using the USEMAP attribute to point to the map definition.
Listing 13.1 shows the example client-side image map definition.
Example 13.1. Using a client-side image map with JavaScript
<html>
<head>
<title>Image Map Example</title>
<script LANGUAGE="JavaScript" type="text/javascript">
function update(t) {
document.form1.text1.value = t;
}
</script>
</head>
<body>
<map NAME="map1">
<area SHAPE=RECT COORDS="14,15,151,87" onClick="update('service')"
onMouseOver="window.status='Service Department'; return true">
<area SHAPE=RECT COORDS="162,16,283,85" onClick="update('sales')"
onMouseOver="window.status='Sales Department'; return true">
<area SHAPE=RECT COORDS="294,15,388,87" onClick="update('info')"
onMouseOver="window.status='Information'; return true">
<area SHAPE=RECT COORDS="13,98,79,178" onClick="update('email')"
onMouseOver="window.status='Email Us'; return true">
<area SHAPE=RECT COORDS="92,97,223,177" onClick="update('products')"
onMouseOver="window.status='Products'; return true">
<area SHAPE=RECT COORDS="235,98,388,177" onClick="update('our staff')"
onMouseOver="window.status='Our Staff'; return true">
<area SHAPE=default onClick="update('No item selected.')"
onMouseOver="window.status='Please select an item.'; return true">
</map>
<h1>Client-Side Image Map Example</h1>
<hr>
The image map below uses JavaScript functions in each of its areas. Moving over
an area will display information about it in the status line. Clicking on an area
places the name of the area in the text field below the image map.
<hr>
<img SRC="imagemap.gif" USEMAP="#map1">
<hr>
<FORM NAME="form1">
<b>Clicked Item:</b>
<input TYPE="text" NAME="text1" VALUE="Please select an item.">
</form>
<hr>
</body>
</html>
This script uses the onMouseOver and onClick event handlers to perform its functions. Rather than link to an actual page, clicking on the areas will display a message in a text field (see Figure 13.1). In addition, the onMouseOver event handlers display a description in the status line for each area.
Figure 13.1 An image map in JavaScript.
This program uses a single JavaScript function called update(), which simply places an item in the text field form1.text1. The text is sent directly by the link in each area definition.
Using Dynamic Images in JavaScript | Next Section

Account Sign In
View your cart