[Chapter 13] The Navigator, Location, and History Objects
Author: David Flanagan
Date: Fri Aug 29 10:59:51 1997

#

| Chapter 13 |

# 13. The Navigator, Location, and History Objects

Contents:
The Navigator, MimeType, and Plugin Objects
The Location Object
The History Object

The Window object contains references to three objects that contain information about the browser or the browser window itself, rather than information about the contents of the window:

- The Navigator object provides version and configuration information about the browser.

- The Location object specifies the URL currently being displayed, and allows JavaScript code to load new URLs.

- The History object contains information about the URLs that have been previously displayed in the window.

This chapter documents each of these Window-related objects.

## 13.1 The Navigator, MimeType, and Plugin Objects

The `Window.navigator` property refers to a Navigator object which contains information about the web browser as a whole (such as the version, and the list of data formats it can display). The Navigator object is named after Netscape Navigator, obviously, but it is also supported (although only partially) by Internet Explorer.

The Navigator object has four properties that provide version information about the browser that is running. The `appName` property contains the name of the browser. The `appVersion` property contains information about the version number and platform of the browser. The `userAgent` property contains the string that the browser sends in its USER-AGENT header in HTTP requests. Finally, the `appCodeName` property contains the "code name" of the browser, which, in general is not particularly useful. Each of these properties is a string in human-readable format, so extracting version information can be a little tricky. See the reference pages for details on the string formats.

In Navigator 3.0, the Navigator object also defines two methods that provide further information about the capabilities of the browser. `javaEnabled()` returns `true` if the browser supports Java, and if it is enabled; otherwise it returns `false`. Similarly, `taintEnabled()` returns `true` if and only if the browser supports a data-tainting security model, and if that model is enabled.

The remaining two properties of the Navigator object are the `mimeTypes[]` array and the `plugins[]` array, which specify the data types that the browser can display and the plug-ins that are installed. These arrays are only available in Navigator 3.0. The subsections below contain more details on these arrays.

### Determining Browser Version Information

We saw above that the Navigator object has four properties that contain information about the browser version. This information is useful when you need to work around bugs in particular versions, or make use of special features found in one browser but not another, for example. Unfortunately, it can be a little difficult to access the information in a convenient way. Example 13.1 shows how you can use the Navigator object to determine what browser is being used, what version of that browser, and what platform it is running on. The code in this example stores the information in more convenient properties of a new `browser` object.

#### Example 13.1: Getting Browser Version Information

    <SCRIPT>
    // Return the version number times 1000. This means that version
    // 2.02 would yield 2020, and version 3.0 would yield 3000.
    // We multiply because Navigator versions 2.0x convert numbers like
    // 2.02 to strings like "2.0199999999875".
    function _get_version()
    {
        return Math.round(parseFloat(navigator.appVersion) * 1000);
    }
    // Figure out the OS we are running on, based on the appVersion property.
    function _get_os()
    {
        if (navigator.appVersion.indexOf("Win95") > 0) return "WIN95";
        else if (navigator.appVersion.indexOf("Win16") > 0) return "WIN31";
        else if (navigator.appVersion.indexOf("Mac") > 0) return "MAC";
        else if (navigator.appVersion.indexOf("X11") > 0) return "UNIX";
        else return "UNKNOWN";
    }
    // Create the object we'll use to store the version information.
    var browser = new Object();
    // First, check if it is a Netscape browser.
    if (navigator.appName.substring(0,8) == "Netscape") {
        // if so, set the name variable appropriately
        browser.name =  "NN";
        // then parse navigator.appVersion to figure out what version
        browser.version = _get_version();
        // Then use appVersion again to determine the OS.
        browser.os = _get_os();
    }
    // Otherwise, see if it is a Microsoft browser.
    //
    // If so, we set all the variables directly, because MSIE only has
    // one JavaScript-enabled version, and it only runs on one platform.
    // We don't use Navigator.appVersion to compute the version number, because
    // it returns a Netscape-compatible value of 2.0 rather than the true
    // MSIE version number 3.0. We don't use it to compute the OS, because
    // MSIE encodes that information with different strings than Navigator
    // does, so we can't use the _get_os() function above.
    //
    // This code will have to be updated when a new version of MSIE is released
    // but we'll have to wait and see how MS encodes the information in the
    // various Navigator object properties before we can update the code.
    else if (navigator.appName.substring(0,9) == "Microsoft") {
        browser.name = "MSIE";
        browser.version = 3000;
        browser.os = "WIN95";
    }
    // Otherwise, it is some unknown browser that supports JavaScript.
    // So we try to guess the browser name, version number and os, assuming
    // that this browser stores the information in the same format as Navigator.
    else {
        browser.name = navigator.appName;
        browser.version = _get_version();
        browser.os = _get_os();
    }
    // Now figure out what version of JavaScript is supported by the browser.
    // Start by assuming that only version 1.0 is supported.
    browser.langlevel = 1000;
    </SCRIPT>
    <SCRIPT LANGUAGE="JavaScript1.1">
    // If the browser supports JavaScript 1.1, update the langlevel variable.
    browser.langlevel = 1100;
    </SCRIPT>
    <SCRIPT LANGUAGE="JavaScript1.2">
    // If the browser supports JavaScript 1.2, update the langlevel variable.
    browser.langlevel = 1200;
    </SCRIPT>

### The MimeType Object

In Navigator 3.0, the `navigator.mimeTypes[]` property is an array of MimeType objects, each of which describe one MIME data format ("text/html", and "image/gif", for example) that the web browser can display (either directly, with an external helper application, or with a plug-in.) The MimeType object itself contains properties that describe the data format.

The `mimeTypes[]` array is indexed numerically, but is also an associative array, indexed by the name of the MIME type. Thus, you can easily check for support of a given data format on the browser:

    // Check to see if the browser can display MPEG files.
    var show_movie = (navigator.mimeTypes["video/mpeg"] != null);

If you want to determine whether a given MIME type is supported by a plug-in (instead of a helper application, for example), you can examine the `enabledPlugin` property of the MimeType object. If it is `null`, then no plug-in supports the object. Otherwise, this property refers to a Plugin object that represents the plug-in that is configured to display data of the specified format.

### The Plugin Object

In Navigator 3.0, the `navigator.plugins[]` property is an array of Plugin objects, each of which represents one plug-in module that has been installed in the browser. The properties of the Plugin object provide various details about the plug-in. The Plugin object also contains array elements, which are a MimeType objects describing each of data formats supported by that particular plug-in. Note that this array is different than the `navigator.mimeTypes[]` array described above.

You can use the `plugins[]` property as an associative array, just as you can the `mimeTypes[]` property. This lets you check for the existence of a particular plug-in without having to loop through the array numerically and check every element:

    // Check to see if the browser has the Shockwave plug-in installed.
    var shocked = (navigator.plugins["Shockwave"] != null);

Other Window Programming Techniques |  | The Location Object