JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

14.4.1 The Navigator Object

14.4.1 The Navigator Object

The navigator property of a Window object refers to a Navigator object that contains browser vendor and version number information. The Navigator object is named after the early Navigator browser from Netscape, but it is also supported by all other browsers. (IE also supports clientInformation as a vendor-neutral synonym for navigator. Unfortunately, other browsers have not adopted this more sensibly named property.)

In the past, the Navigator object was commonly used by scripts to determine if they were running in Internet Explorer or Netscape. This “browser-sniffing” approach is problematic because it requires constant tweaking as new browsers and new versions of existing browsers are introduced. Today, feature testing (see §13.4.3 ) is preferred: rather than making assumptions about particular browser versions and their features, you simply test for the feature (i.e., the method or property) you need.

Browser sniffing is sometimes still valuable, however, such as when you need to work around a specific bug that exists in a specific version of a specific browser. The Navigator object has four properties that provide information about the browser that is running, and you can use these properties for browser sniffing:


The full name of the web browser. In IE, this is “Microsoft Internet Explorer”. In Firefox, this property is “Netscape”. For compatibility with existing browser-sniffing code, other browsers often report the name “Netscape” as well.


This property typically begins with a number and follows that with a detailed string that contains browser vendor and version information. The number at the start of this string is often 4.0 or 5.0 to indicate generic compatibility with fourth- and fifth-generation browsers. There is no standard format for the appVersion string, so parsing it in a browser-independent way isn’t possible.

userAgent The string that the browser sends in its USER-AGENT HTTP header. This property typically contains all the information in appVersion and may contain additional details as well. Like appVersion, there is no standard format. Since this property contains the most information, browser-sniffing code typically uses it.


A string that identifies the operating system (and possibly the hardware) on which the browser is running.

The complexity of the Navigator properties demonstrates the futility of the browser-sniffing approach to client-side compatibility. In the early days of the Web, lots of browser-specific code was written that tested properties like navigator.appName. As new browsers were written, vendors discovered that in order to correctly display existing websites, they had to set the appName property to “Netscape”. A similar process caused the number at the start of the appVersion to lose meaning, and today browser-sniffing code must rely on the navigator.userAgent string and is more complicated than it once was. Example 14-3 shows how to use regular expressions (from jQuery) to extract the browser name and version number from navigator.userAgent.

Example 14-3. Browser sniffing using navigator.userAgent

// Define and browser.version for client sniffing, using code // derived from jQuery 1.4.1. Both the name and number are strings, and both // may differ from the public browser name and version. Detected names are: // // "webkit": Safari or Chrome; version is WebKit build number // "opera": the Opera browser; version is the public version number // "mozilla": Firefox or other gecko-based browsers; version is Gecko version // "msie": IE; version is public version number // // Firefox 3.6, for example, returns: { name: "mozilla", version: "1.9.2" }. var browser = (function() {

var s = navigator.userAgent.toLowerCase(); var match = /(webkit)[ \/]([\w.]+)/.exec(s) || /(opera)(?:.*version)?[ \/]([\w.]+)/.exec(s) || /(msie) ([\w.]+)/.exec(s) || !/compatible/.test(s) && /(mozilla)(?:.*? rv:([\w.]+))?/.exec(s) || [];

14.4 Browser and Screen Information | 347

return { name: match[1] || "", version: match[2] || "0" }; }());

In addition to its browser vendor and version information properties, the Navigator object has some miscellaneous properties and methods. The standardized and widely implemented nonstandard properties include:


The navigator.onLine property (if it exists) specifies whether the browser is cur

rently connected to the network. Applications may want to save state locally (using

techniques from Chapter 20 ) while they are offline.


A Geolocation object that defines an API for determining the user’s geographical location. See §22.1 for details.

javaEnabled() A nonstandard method that should return trueif the browser can run Java applets.


A nonstandard method that should return true if the browser can store persistent

cookies. May not return the correct value if cookies are configured on a site-by-site


友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1