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 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
- Creating a Dynamic Site Map
- Creating Drop-Down Menus
- Workshop: Creating a Scrolling Text Box
- Summary
- Q&A
- Quiz
Exercises
- 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
Creating Drop-Down Menus
Another common use for DHTML is to create drop-down menus. Similar to the previous example, this can be accomplished using layers, defined with <div> tags, and manipulating their visibility. This time you'll use the visibility property to hide or show the layers.
Creating the HTML Document
The HTML document for the drop-down menu example is shown in Listing 23.2. This document uses a <table> section to define the menu terms, and <div> tags to define the menus that will appear when you move over each menu term.
Example 23.2. The HTML Document for the Drop-Down Menu Example
<html>
<head>
<title>Drop-Down Menu Example</title>
<script language="JavaScript" type="text/javascript" src="menu.js">
</script>
</head>
<body style="margin-left:0; margin-top:0">
<table border="0" cellpadding="4">
<tr>
<td ID="menu-products" width="100" bgcolor="Silver"
onMouseOver="Menu('products')" onMouseOut="Timeout('products')">
<a href="products.html"><b>Products</b></a>
</td>
<td ID="menu-sales" width="100" bgcolor="Silver"
onMouseOver="Menu('sales')" onMouseOut="Timeout('sales')">
<a href="sales.html"><b>Sales</b></a>
</td>
<td ID="menu-service" width="100" bgcolor="Silver"
onMouseOver="Menu('service')" onMouseOut="Timeout('service')">
<a href="service.html"><b>Service</b></a>
</td>
</tr>
</table>
<div ID="products" STYLE="position:absolute; visibility: hidden">
<table width="100%" border="0" cellpadding="4" cellspacing="0">
<tr> <td width="100%" ID="p1"
onMouseOver="Highlight('products','p1')"
onMouseOut="UnHighlight('products','p1')">
<a href="equip.html">Equipment</a></td></tr>
<tr> <td width="100%" ID="p2"
onMouseOver="Highlight('products','p2')"
onMouseOut="UnHighlight('products','p2')">
<a href="supplies.html">Supplies</a></td></tr>
</table>
</div>
<div ID="sales" STYLE="position:absolute; visibility: hidden">
<table width="100%" border="0" cellpadding="4" cellspacing="0">
<tr> <td width="100%" ID="s1"
onMouseOver="Highlight('sales','s1')"
onMouseOut="UnHighlight('sales','s1')">
<a href="prices.html">Price List</a></td></tr>
<tr> <td width="100%" ID="s2"
onMouseOver="Highlight('sales','s2')"
onMouseOut="UnHighlight('sales','s2')">
<a href="order.html">Order Form</a></td></tr>
<tr> <td width="100%" ID="s3"
onMouseOver="Highlight('sales','s3')"
onMouseOut="UnHighlight('sales','s3')">
<a href="specials.html">Specials</a></td></tr>
</table>
</div>
<div ID="service" STYLE="position:absolute; visibility: hidden">
<table width="100%" border="0" cellpadding="4" cellspacing="0">
<tr> <td width="100%" ID="r1"
onMouseOver="Highlight('service','r1')"
onMouseOut="UnHighlight('service','r1')">
<a href="support.html">Support</a></td></tr>
<tr> <td width="100%" ID="r2"
onMouseOver="Highlight('service','r2')"
onMouseOut="UnHighlight('service','r2')">
<a href="cservice.html">Contact Us</a></td></tr>
</table>
</div>
<h1 align="center">Drop-Down DHTML Menus</h1>
<p>This is a basic test of drop-down DHTML menus. To
test the menus, move the mouse over the menu items above.
</p>
</body>
</html>
The menu terms use the onMouseOver event handler to call the Menu function, which will show a menu, and the onMouseOut event handler to call the Timeout function, which will erase the menu after a timeout.
Since this is a complex example, the JavaScript functions will be included in a separate file. Notice that the <script> tag in the header has a src attribute that refers to the file menu.js. For now, save the HTML document.
Creating the JavaScript Functions
The JavaScript functions for this example are stored in the separate menu.js file. Listing 23.3 shows the complete listing for the JavaScript file.
Example 23.3. The JavaScript Functions for the Drop-Down Menus
var inmenu=false;
var lastmenu=0;
function Menu(current) {
if (!document.getElementById) return;
inmenu=true;
oldmenu=lastmenu;
lastmenu=current;
if (oldmenu) Erase(oldmenu);
m=document.getElementById("menu-" + current);
box=document.getElementById(current);
box.style.left= m.offsetLeft;
box.style.top= m.offsetTop + m.offsetHeight;
box.style.visibility="visible";
m.style.backgroundColor="Aqua";
box.style.backgroundColor="Aqua";
box.style.width="108px";
}
function Erase(current) {
if (!document.getElementById) return;
if (inmenu && lastmenu==current) {
return;
}
m=document.getElementById("menu-" + current);
box=document.getElementById(current);
box.style.visibility="hidden";
m.style.backgroundColor="Silver";
}
function Timeout(current) {
inmenu=false;
window.setTimeout("Erase('" + current + "')",500) ;
}
function Highlight(menu,item) {
if (!document.getElementById) return;
inmenu=true;
lastmenu=menu;
obj=document.getElementById(item);
obj.style.backgroundColor="Silver";
}
function UnHighlight(menu,item) {
if (!document.getElementById) return;
Timeout(menu);
obj=document.getElementById(item);
obj.style.backgroundColor="Aqua";
}
This document defines the Menu and Timeout functions called by the event handlers, the Erase function to erase a menu, and the Highlight and UnHighlight functions to highlight the items in each menu as you move over them.
Figure 23.2 shows the menu in action. In the figure, the Products menu is currently displayed.
Figure 23.2 The Drop-Down menu example in action.
Workshop: Creating a Scrolling Text Box | Next Section

Account Sign In
View your cart