Professional JavaScript | 40 | WebReference

Professional JavaScript | 40

To page 1current pageTo page 3
[previous] [next]

Professional JavaScript

Compatibility Techniques

Unfortunately, JavaScript browsers are not unequal but different as well. Netscape and Microsoft have been diverging in their advanced features as fast as possible in order to obtain a loyal customer base. Today's advanced features are tomorrow's backwards-compatibility nightmares.

Handling compatibility requires a two-pronged attack: readership strategies and technical tricks.

Choose Readers

If you only intend to use JavaScript to set browser preferences, then readership isn't much of an issue. However, JavaScript is mostly embedded in HTML documents. Who is going to try and read these pages? Answer these questions and avoid masses of work being tripped up by compatibility issues.

If none of the above is the case, then you are left with multiple JavaScript/browser versions to handle. You can still simplify matters, provided you don't mind losing some readership or having your content look bad in unexpected places:

A safe middle ground at this time of writing is to use the features in Netscape 3.0 JavaScript. Failing all that, you can spend a lot of time making your JavaScript work everywhere and gracefully downgrade everywhere else. Lots and lots of time.

Choose Tricks

Here are a variety of tricks for handling different browsers in JavaScript.

Supporting Ancient Browsers
<!-- hide from old, stupid browsers
document.write('JavaScript rules, but only here!');
// end comment --> 

Shown earlier, comments hide script source from browsers that don't know <SCRIPT>.

Detecting Scripts are Ignored
 alert("Off to the JavaScript page?");
 document.location.href = 'js_top.htm';
<NOSCRIPT>Get a real browser, see it all!</NOSCRIPT>

The <NOSCRIPT> tag provides a method of dealing with browsers, which refuse to interpret JavaScript, either because they can't or because the user has turned JavaScript off. The main use of this tag is to provide some feedback to the user asking them to go away or else enable JavaScript. It can be used, as in this example, to show a dummy page that is replaced if JavaScript is enabled. Although the alert in this case stops the 'real' page from loading automatically, it can easily be left out.

To page 1current pageTo page 3
[previous] [next]

Created: April 23, 2001
Revised: April 23, 2001