Приглашаем посетить
Бунин (bunin-lit.ru)

The ClientInformation or Navigator Property

The clientInformation or navigator Property

The clientInformation and navigator properties reference an object containing information about the client. The clientInformation property was added to Internet Explorer 4.0 as an alias for navigator to separate any implied relationship between the object model and a particular browser. However, because the clientInformation property is currently supported only by Internet Explorer 4.0, you should use the navigator property when you are targeting multiple browsers. Both properties return the same information, including the name and version of the client.


NOTE: Throughout this section, the clientInformation and navigator properties and objects are used interchangeably. In all cases, both properties and objects provide exactly the same information.

Using the clientInformation or navigator object, code can be executed conditionally based on the browser brand or version number. If you are simply creating code to work around a bug or a small unsupported feature in one browser, client-side checking works well. But client-side negotiation that results in new pages being downloaded requires multiple hits to the server. If you want to provide different pages for each brand of browser, you can get better performance by transmitting the correct page initially, based on the header that is submitted.

Client Brand Information

The following four properties on the navigator object expose the client version and name:

Both Internet Explorer and Netscape Navigator follow the same general format for the appVersion property. The appVersion property returns the version of the client in the following format:

clientVersion (platform; information [; extraInformation])

In Netscape Navigator, the platform field indicates which platform the browser is running on. In Internet Explorer versions 3.0 and 4.0, the string compatible is returned as the platform, and the actual platform is indicated in the extraInformation field.

In Netscape Navigator, the information field indicates the product's encryption level. For example, I is returned for the international release, which provides a weaker level of security than the U.S. version in order to comply with U.S. export restrictions. The U.S. release returns U in this field. Internet Explorer returns a version number in the information field.

The extraInformation field may return the platform or the build number of the required operating system. Internet Explorer uses this field to return detailed platform information. Depending on the platform, this field may or may not be used by Netscape Navigator.

In general, the fields in the appVersion property follow a consistent format. Your code can distinguish between the different clients by parsing the value of this property. The following table lists the values returned by Internet Explorer and Netscape Navigator on the Microsoft Windows platform for the appCodeName, appName, and appVersion properties.


Browser appCodeName appName appVersion
Microsoft Internet Explorer 3.0 Mozilla Microsoft Internet Explorer 2.0 (compatible; MSIE 3.0A; Windows 95)
Microsoft Internet Explorer 4.0 Mozilla Microsoft Internet Explorer 4.0 (compatible; MSIE 4.0; Windows 95)
Netscape Navigator 2.01 Mozilla Netscape 2.01 (Win95; I)
Netscape Navigator 3.0 Mozilla Netscape 3.0 (Win95; I)
Netscape Navigator 4.0 Mozilla Netscape 4.0 (Win95; I)

The userAgent property contains the HTTP (Hypertext Transfer Protocol) user-agent string that was specified in the HTTP request. The user-agent string is just the concatenation of the appCodeName property and the appVersion property, separated by a slash: appCodeName/appVersion.

Parsing appVersion

The code on the following page parses the appVersion property into its basic components. The individual elements of appVersion are then added to the navigator object as properties.

<HTML>
   <HEAD>
      <TITLE>Application Version</TITLE>
      <SCRIPT LANGUAGE="JavaScript">
         // Initialize version.
         function getVersionInfo() {
            var version = navigator.appVersion;

            // Locate the opening (.
            var iParen = version.indexOf("(", 0);

            // The client version is the string before the (.
            navigator.clientVersion = 
               version.substring(0, iParen - 1);
            var information = new Array();

            // Automatically split the remaining values into an array.
            information = version.substring(iParen + 1,
               version.length - 1).split(";");

            // First value is the platform.
            navigator.platform = information[0];

            // Second value is the information field.
            navigator.information = information[1];

            /* Third value is extra information, which may be null
               depending on the browser and platform. */
            navigator.extraInformation = information[2];
         }
         getVersionInfo();
      </SCRIPT>
   </HEAD>
   <BODY>
      <H1>Client Information</H1>
      <SCRIPT LANGUAGE="JavaScript">
         // Output the information.
         document.write("Platform: " + navigator.platform + "<BR>");
         document.write("Client Version: " + navigator.clientVersion + 
            "<BR>");
         document.write("Information: " + navigator.information + 
            "<BR>");
         document.write("Extra Info: " + navigator.extraInformation + 
            "<BR>");
      </SCRIPT>
   </BODY>
</HTML>

Multiple Windows and the navigator Object

The navigator object is not shared between all loaded instances of the window. Instead, each window has its own instance of the navigator object. While in almost all cases the information exposed by the navigator object is the same for each window, this isolation is important. For security reasons, if an instance of a page customizes the navigator object, only that instance is allowed to access it.

The preceding code parses the client information into multiple properties that are added directly to the navigator object. These properties are available only on the navigator object of the associated window. Referencing these user-defined properties of the navigator object on another window will return undefined values, as they do not exist.

User Settings

The navigator object provides access to information about whether Java and cookies are enabled or disabled on the user's browser.

Java Support

To determine whether Java is enabled on the client, the navigator object exposes a javaEnabled method. This method returns a Boolean value that specifies whether the client can display Java applets.

Using the javaEnabled method, you can write a script that either inserts the applet or displays a message to the user:

<SCRIPT LANGUAGE="JavaScript">
   if (navigator.javaEnabled()) 
      document.write("<APPLET NAME=demo CODE=demo.class " +
         "WIDTH=50 HEIGHT=50></APPLET>");
   else
      document.write("<B>This page cannot run with Java disabled." +
         "</B>");
</SCRIPT>

Cookie Support

Internet Explorer 4.0 exposes a property, cookieEnabled, that specifies whether the client supports cookies. Cookies allow a small piece of information that is associated with the current URL or domain to be retained on the client's machine. Some users do not want pages to retain any information on their hard drives and so disable this browser feature. The cookieEnabled property contains a Boolean value that indicates whether the user has deactivated this support. Using this property, you can write custom behavior that does not rely on the client-side cookie if it is unavailable.

Unfortunately, Internet Explorer 3.0 and Netscape Navigator 4.0 do not support the cookieEnabled property. Therefore, this method cannot always be relied on for determining whether cookies are enabled on the client machine.

New navigator Object Properties

The navigator object supports a number of new properties that you can use to adapt your document to different users. For example, you can write a script to check the connectionSpeed property and download a large image only if the user has a relatively high bandwidth. The following table summarizes these properties.


Property Description
cpuClass The type of CPU. The value for a Pentium machine is x86.
systemLanguage The default language for the system. For American English the value is en-us.
userLanguage The user's default language. For American English the value is en-us.
operatingSystem The user's current operating system. For Microsoft Windows 95 the value is win32.
appMinorVersion The minor version of the browser application. The value for Internet Explorer 4.0 is 0.
connectionSpeed The average connection speed if available, -1 otherwise.
onLine A Boolean value indicating whether the user is reading the page online.

[Содержание]