Javascript [Electronic resources] : The Definitive Guide (4th Edition) نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Javascript [Electronic resources] : The Definitive Guide (4th Edition) - نسخه متنی

David Flanagan

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید


13.6 The Navigator Object


The Window.navigator property refers to a
Navigator object that contains
information about the web browser as a whole, such as the version and
a list of the data formats it can display. The Navigator object is
named after Netscape Navigator, but it is also supported by
Internet Explorer. IE also
supports
clientInformation as a vendor-neutral synonym for
navigator. Unfortunately, Netscape and Mozilla do
not support this property.

The Navigator object has five main properties that provide
version
information about the browser that is running:

appName

The simple
name
of the web browser.

appVersion

The version number and/or other version information for the browser.
Note that this should be considered an "internal" version
number, since it does not always correspond to the version number
displayed to the user. For example, Netscape 6 reports a version
number of 5.0, since there never was a Netscape 5 release. Also, IE
Versions 4 through 6 all report a version number of 4.0, to indicate
compatibility with the baseline functionality of fourth-generation
browsers.

userAgent

The
string that the browser sends in its USER-AGENT
HTTP header. This property typically contains all the information in
both appName and appVersion.

appCodeName

The
code name of the browser. Netscape uses the code name
"Mozilla" as the value of this property. For
compatibility, IE does the same thing.

platform

The
hardware platform on which the browser is running. This property was
added in JavaScript 1.2.

The following lines of JavaScript code display each of these
Navigator object properties in a dialog box:

var browser = "BROWSER INFORMATION:\n";
for(var propname in navigator) {
browser += propname + ": " + navigator[propname] + "\n"
}
alert(browser);

Figure 13-2 shows the dialog box displayed when the
code is run on IE 6.


Figure 13-2. Navigator object properties


As you can see from Figure 13-2, the properties of
the Navigator object have values that are sometimes more complex than
we are interested in. We are often interested in only the first digit
of the appVersion property, for example. When
using the Navigator object to test browser information, we often use
methods such as parseInt( ) and
String.indexOf( ) to extract only the information
we want. Example 13-3 shows some code that does this:
it processes the properties of the Navigator object and stores them
in an object named browser. These properties, in
their processed form, are easier to use than the raw
navigator properties. The general term for

code like this is a
"client sniffer," and you can find more complex and
general-purpose sniffer code on the Internet.[1] For many purposes, however, something as simple as that
shown in Example 13-3 works just fine.

[1] See,
for example, http://www.mozilla.org/docs/web-developer/sniffer/browser_typel.

Example 13-3. Determining browser vendor and version

/*
* File: /image/library/english/10113_browser.js
* Include with: <script SRC="/image/library/english/10113_browser.js"></script>
*
* A simple "sniffer" that determines browser version and vendor.
* It creates an object named "browser" that is easier to use than
* the "navigator" object.
*/
// Create the browser object
var browser = new Object( );
// Figure out the browser's major version
browser.version = parseInt(navigator.appVersion);
// Now figure out if the browser is from one of the two
// major browser vendors. Start by assuming it is not.
browser.isNetscape = false;
browser.isMicrosoft = false;
if (navigator.appName.indexOf("Netscape") != -1)
browser.isNetscape = true;
else if (navigator.appName.indexOf("Microsoft") != -1)
browser.isMicrosoft = true;

/ 844