please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 13 Next

 


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:

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);

Previous Home Next
Other Window Programming Techniques Book Index The Location Object


HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/ch13_01.htm, 13KB, , updated: 2019/10/14 15:00, local time: 2024/11/20 00:42,
TOP NEW HELP FIND: 
3.133.138.234:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://massmind.org/techref/language/java/script/definitive/ch13_01.htm"> [Chapter 13] The Navigator, Location, and History Objects</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .