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
- Reading Browser Information
- Supporting Browsers with JavaScript
- Supporting Non-JavaScript Browsers
- Workshop: Scripting for Multiple Browsers
- Summary
- Q&A
- Quiz
- Exercises
- 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
Workshop: Scripting for Multiple Browsers
As an example of the techniques you learned in this hour, you can create a script that uses reliable methods to determine which of the following categories the current browser falls into:
- Either Internet Explorer or Netscape, 5.0 or later
- Netscape 4.x
- Internet Explorer 4.x
- All others
This is useful because the 5.0 and later browsers support the new DOM for dynamic HTML. You may also want to support the 4.x browsers, which support dynamic features but in incompatible ways. Last but not least, older browsers can be supported with a minimal script.
As a simple example, this script will determine the browser in use and set a variable, browser, to the appropriate code. Listing 14.2 shows the browser detection example.
Example 14.2. The browser detection example
<html>
<head>
<title>Browser Detection</title>
<script language="Javascript" type="text/javascript">
// check for 5.0 or later browsers
if (parseInt(navigator.appVersion) >= 5
|| navigator.appVersion.indexOf("MSIE 5") != -1) {
browser="DOM";
} else if (navigator.userAgent.indexOf("Mozilla/4") != -1)
{
if (navigator.appName.indexOf("Netscape") != -1)
browser="NS4";
if (navigator.appVersion.indexOf("MSIE 4") != -1)
browser="IE4";
} else browser="Other";
</script>
</head>
<body>
<h1>Browser Detection Example</h1>
<script language="Javascript" type="text/javascript">
document.write("browser detected: " + browser + "<br>");
</script>
</body>
</html>
The script in the header of this document assigns the variable browser the value of DOM for 5.0 browsers, NS4 for Netscape 4.x, and IE4 for Internet Explorer 4.x. The body of this document simply displays the variable, but your script could use it to change the output according to the browser.
Summary | Next Section

Account Sign In
View your cart