Sams Teach Yourself JavaScript in 24 Hours

Sams Teach Yourself JavaScript in 24 Hours

By Michael Moncur

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:

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.

Share ThisShare This

Informit Network