Sams Teach Yourself JavaScript in 24 Hours

Sams Teach Yourself JavaScript in 24 Hours

By Michael Moncur

Supporting Browsers with JavaScript

If you're using features that are on the cutting edge and require a particular browser or version, there are several ways of using JavaScript to steer users in the right direction. The following sections explain three different methods.

Creating a Browser-Specific Page

The easiest solution—and the least polite—is to pick a browser version to support and kick everyone out who isn't using it. This is easy to do with a script in the header or body of the page. Here's a simple example:

<script language="JavaScript" type="text/javascript">
if (navigator.appName.indexOf("Netscape") == -1
|| navigator.appVersion.indexOf("5.0") == -1)
window.alert("Download Netscape now, or else.");
window.location = "http://www.netscape.com/";
</script>

If you're using a non-Netscape browser or a version of Netscape other than 5.0/6.0, this script displays a rude message and sends you to Netscape's Web page. Needless to say, there are more polite ways of saying this.

Branching to Separate Pages

In most cases, it's not hard to support more than one browser. If you want to fine-tune your pages to display perfectly on both Netscape and IE, one convenient way is to create a separate version of the page for each browser. You can then use JavaScript to send the user to the appropriate page.

For example, here is a script that could be placed on the introductory page of a site. After detecting the browser version, it sends the user to the appropriate page.

<script language="Javascript" type="text/javascript">
if (navigator.appName.indexOf("Netscape") > -1
 && navigator.appVersion.indexOf("5") > -1)
    window.location = "netscape.html";
if (navigator.appName.indexOf("Microsoft") > -1
 && navigator.appVersion.indexOf("4") > -1)
    window.location = "ie.html";
window.location = "default.html";
</SCRIPT>

This script sends the user to one of three pages: netscape.html for Netscape 5.x or 6.x, ie.html for Internet Explorer 4.x or later, or default.html for any other browser.

Making a Multiple-Browser Page

The third alternative is to make a single page that supports more than one type of browser. You can use a script in the page to detect the browser, and then use script commands to include different HTML codes in the document depending on the browser.

This method uses only one document, but you might find that it transforms a long, complicated page into a long, impossibly complicated page. A short example of a page that does this is included in this hour's Workshop section.

Using Feature Sensing

The examples so far in this hour have used browser sensing to differentiate between browsers: They look at the navigator object's properties to determine which browser is in use. While this is sometimes the only way to support all browsers, feature sensing is often a better way.

Feature sensing detects whether a feature is supported, rather than being concerned with the exact browser in use. For example, if your script uses dynamic images, you could use this statement to determine whether they are supported:

if (document.images) alert("dynamic images are supported.");

This statement checks for the existence of the images array. If it exists, the browser most likely supports dynamic images—if it doesn't exist, this statement won't cause an error and you'll know dynamic images aren't supported.

Feature sensing has some advantages: First, it doesn't rely on specific browser versions, so a new browser release is less likely to break your script. Second, it's often easier than dealing with all of the possible navigator properties used in different browsers.

Share ThisShare This

Informit Network