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 a Dynamic Site Map
One of the most popular uses for DHTML is to create dynamic, tree-like navigation maps for sites, with items that can be expanded and collapsed. You can create a dynamic site map easily using <div> tags to define layers and a simple script.
Defining the Layers
Each category in the site map will be defined with a link to the Toggle function, which you'll define later. The linked text is a symbol, [+] to indicate that the item can be expanded or [-] to indicate that it can be collapsed.
<b><a ID="xsupport" href="javascript:Toggle('support')">[+]</a>
Support</b><br>
<div ID="support" style="display:none; margin-left:2em">
<a href="sforum.html">Support Forum</a><br>
<a href="sforum.html">Contact Support</a><br>
</div>
Creating the Script
Each collapsible section of the site map will be defined using a <div> tag. The Toggle function uses the style.display property to expand or collapse a specified item:
<script language="javascript" type="text/javascript">
function Toggle(item) {
obj=document.getElementById(item);
visible=(obj.style.display!="none")
key=document.getElementById("x" + item);
if (visible) {
obj.style.display="none";
key.innerHTML="[+]";
} else {
obj.style.display="block";
key.innerHTML="[-]";
}
}
</script>
This function first uses the visible variable to indicate whether the item is currently visible. If the object is visible, its display property is set to none to collapse it, and the object's indicator is changed to a [+] symbol. If the object is currently hidden, it is displayed and the indicator is changed to [-].
Creating the HTML Document
To use this function to create the dynamic site map, you can include the script in an HTML document and use <div> tags to define the map sections. Listing 23.1 shows the complete HTML document.
Example 23.1. Creating a DHTML Site Map
<html>
<head><title>Creating a Navigation Tree</title>
<style>
A {text-decoration: none;}
</style>
<script language="javascript" type="text/javascript">
function Toggle(item) {
obj=document.getElementById(item);
visible=(obj.style.display!="none")
key=document.getElementById("x" + item);
if (visible) {
obj.style.display="none";
key.innerHTML="[+]";
} else {
obj.style.display="block";
key.innerHTML="[-]";
}
}
</script>
</head>
<body>
<h1>Navigation Tree Example</h1>
<p>The navigation tree below allows you to expand and
collapse items. You could use this in a frame to provide a
sophisticated navigation system for a site.</p>
<hr>
<b><a ID="xproducts" href="javascript:Toggle('products')">[+]</a>
Products</b><br>
<div ID="products" style="display:none; margin-left:2em">
<a href="prodlist.html">Product List</a><br>
<a href="order.html">Order Form</a><br>
<a href="pricelist.html">Price List</a><br>
</div>
<b><a ID="xsupport" href="javascript:Toggle('support')">[+]</a>
Support</b><br>
<div ID="support" style="display:none; margin-left:2em">
<a href="sforum.html">Support Forum</a><br>
<a href="sforum.html">Contact Support</a><br>
</div>
<b><a ID="xcontact" href="javascript:Toggle('contact')">[+]</a>
Contact Us</b>
<div ID="contact" style="display:none; margin-left:2em">
<a href="contact1.html">Service Department</a><br>
<a href="contact2.html">Sales Department</a><br>
</div>
</body>
</html>
This document incorporates the Toggle function and a complete set of links and <div> sections to define the expandable menu. Figure 23.1 shows the site map in action.
Figure 23.1 The dynamic site map in action.
Creating Drop-Down Menus | Next Section

Account Sign In
View your cart