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
- 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
Planning for the Future
In the history of JavaScript, there has never been such a major change to the language that a great number of scripts written using the older version have stopped working. Nevertheless, many scripts have been crippled by new releases—chiefly those that used browser-specific features.
The following sections offer some guidelines you can follow in writing scripts to ensure that the impact of future JavaScript versions and browser releases will be minimal.
Keeping Scripts Compatible
Right now, dynamic HTML (DHTML) is in use on many Web pages. The new W3C DOM standardizes the objects used for dynamic HTML, but it was not available when the vast majority of these pages were created. Thus, they use a number of techniques to offer the latest dynamic features:
- Browser detection is used to separately support Netscape and Internet Explorer, or in some cases a specific browser is required.
- Scripts are written to work around bugs in browsers, or sometimes even take advantage of them.
- The process of writing scripts often involves trial and error rather than consulting official documentation.
While the W3C DOM will solve these issues when it is widely supported, there will always be brand new features that are not part of the official DOM specification and will be subject to change.
There's nothing wrong with using these features—but if you do, you should be aware that you're going to need to test the scripts on several different browsers. Additionally, if you've used non-standard features, you will eventually have to rewrite the scripts to use newer, standard features.
Staying HTML Compliant
One trend as browsers advance is that newer browsers tend to do a better job of following the W3C standard for HTML—and often, relying on it. This means that while a page that uses completely standard HTML will likely work in future browsers, one that uses browser-specific features or workarounds is bound to have problems eventually.
To avoid these problems, try to use completely valid HTML whenever possible. This means not only using standard tags and attributes, but following certain formatting rules: For example, always using both opening and closing <p> tags, and enclosing numbers for table widths and other parameters in quotation marks.
To be sure your documents follow the HTML standard, see Appendix B,"Tools for JavaScript Developers," for suggested HTML validation programs and services. These will examine your document and point out any areas that do not comply with the HTML standard.
Document Everything
Last but not least, be sure you understand everything your scripts are doing. Document your scripts using comments, and particularly document any statements that may look cryptic or were particularly hard to get working properly.
If your scripts are properly documented, it will be a much easier process if you have to modify them to be compatible with a future browser, JavaScript, or DOM version.
A few JavaScript Tips | Next Section

Account Sign In
View your cart