The Navigator property of the Window object refers to the Navigator object that contains the browser manufacturer and version information. The Navigator object was named to commemorate the Netscape after Navigatorbu 2, but all other browsers also support it (ie also supports clientln formation attribute, which is synonymous with navigator's vendor neutrality
Word. Unfortunately, other browsers do not support this more intuitive property naming. In the past, navigator objects were often used by scripts to determine whether they were running in IE or in Netscape. This browser sniffing method is problematic because it requires the introduction of new browsers and new versions of existing browsers to continually
Adjustment. Today, there is a better method of functional testing (see section 13.4.3), which requires only testing the required functionality (i.e., methods or properties) rather than setting up specific browser versions and their functions. However, browser sniffing is still valuable at times. One such scenario is when you need to address a particular
Special bug in a specific version of the browser. The Navigator object has 4 properties that provide information about the version of the browser that is running, and you can use these properties for browser sniffing.
AppName
The full name of the Web browser. In IE, this is Microsoft Internet Explorer. In Firefox, this property is "Netscape". In order to be compatible with existing browser sniffing code, other browsers often also value "Netscape".
AppVersion
This property usually starts with a number and follows the detailed string containing the browser manufacturer and version information. The number in front of the string is usually 4.0 or 5.0, indicating that it is a 4th or 5th generation compatible browser. The appversion string does not have a standard format, so there is no way to use it directly to determine the type of browser.
UserAgent
Browser The string that is sent in its user-agent HTTP header. This property usually contains all the information in the appversion, and often may contain other details. And appversion--like, it also has no standard format. Because this property contains most of the information, browser sniffing code usually uses it to sniff.
Platform
The string on which the browser's operating system (and possibly hardware) is running. The complexity of the Navigator attribute is an indication that browser sniffing is not very helpful in dealing with client compatibility issues. In the early days of the web, people wrote a lot of browser-specific code for testing similar to Navigator.appname
's Properties. When developing a new browser, browser vendors find that to make the existing sites appear correct, they need to set the appname to "Netscape". Similar practices make AppVersion's starting numbers meaningless, and now browser sniffing code must rely on navigator.useragent strings that are much more complex than before.
Example 14-3 shows how to use regular expressions (excerpt from IQuery) from navigator. The method of extracting the browser name and version number from the useragent.
Example 14-3: Browser sniffing using navigator.useragent~-//For client sniffing define Browser.name and browser.version, where the code in Jouery l.4.1 is used// Both the name and number are strings, and the results for different browser outputs are not the same, and the results are as follows: j|//"WebKit": Safari or Chrome; version number is WebKit version number//"opera": Opera; Version number is the version number of the SOFTWARE//"Mozilla": Firefox or other browser based on Gecko kernel, version number is Gecko Version//"MSIE": IE; version number is the version of the software | | For example, Firefox 3.6 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) &&/(?:. *RV: ([\w.] +))?/.exec (s) I l[];
JS's Navigator Object