BOM navigator object and user agent detection, bomnavigator

Source: Internet
Author: User

BOM navigator object and user agent detection, bomnavigator

Previous

The navigator object has now become a de facto standard for identifying browser clients. The navigator object is common to all browsers that support javascript. This article describes the navigator object and User Agent Detection in detail.

Attribute

Similar to other BOM objects, the navigator objects in each browser also have their own attributes. The following table lists the attributes and methods that exist in all browsers and the versions that support them.

Attribute description

AppCodeName browser name [all browsers return Mozilla]

User proxy string of the userAgent Browser

AppVersion browser version

AppMinorVersion: version number [0 returned by IE, not supported by chrome or firefox]

System platform where the platform browser is located [all browsers return Win32]

Array of plug-in information installed in the plugins Browser

MimeTypes MIME type array registered in the browser

Language main language of the browser [IE10-not supported, other browsers return zh-CN]

SystemLanguage operating system language [IE returns zh-CN, chrome and firefox are not supported]

UserLanguage: default Operating System language [zh-CN returned by IE, not supported by chrome or firefox]

Product name [IE10-not supported, other browsers return Gecko]

ProductSub product secondary information [IE not supported, chrome returns 20030107, firefox returns 20100101]

Brand of the vendor browser [chrome returns to Google Inc., IE and firefox are not supported]

Whether onLine is connected to the Internet [IE returns true or false based on actual conditions, chrome and firefox always return true]

CookieEnabled indicates whether the cookie is enabled. [true is returned for all browsers]

Whether javaEnabled enables java [IE8-return by browser {}; return by other browsers function javaEnabled ()]

BuildID: Version compiled by the browser [firefox returns 20170125094131, chrome and IE are not supported]

CpuClass the CPU type used by the computer [IE returns x86, chrome and firefox do not support]

Oscpu OS or CPU used [Windows NT 10.0 is returned for firefox; WOW64, chrome and IE are not supported]

Detection plugin

Browser plugin detection is the most common detection routine.

For non-ie browsers, you can use the plugins array to achieve this goal. Each item in this array contains the following attributes:

Name: plug-in name

Description: plug-in description

Filename: plug-in file name

Length: Number of MIME types processed by the plug-in

Iterate Each plug-in cyclically and compare the plug-in name with the given name

Function hasPlugin (name) {name = name. toLowerCase (); for (var I = 0; I <navigator. plugins. length; I ++) {if (navigator. plugins [I]. name. toLowerCase (). indexOf (name)>-1) {return true ;}}// detect flashconsole. log (hasPlugin ("Flash"); // true

For ie browsers, the method for detecting plug-ins is to use the proprietary ActiveXObject type and try to create an instance of a specific plug-in. IE uses COM objects to implement plug-ins, while COM objects are identified by unique identifiers. Therefore, to check a specific plug-in, you must know its COM identifier. For example, the identifier of Flash is ShockwaveFlash. ShockwaveFlash.

Function hasIEPlugin (name) {try {new ActiveXObject (name); return true ;}catch (ex) {return false ;}// detects the Flashconsole. log (hasIEPlugin ("ShockwaveFlash. shockwaveFlash ") // true

[Compatible writing]

// Check the plug-in function hasPlugin (name) {name = name. toLowerCase (); for (var I = 0; I <navigator. plugins. length; I ++) {if (navigator. plugins [I]. name. toLowerCase (). indexOf (name)>-1) {return true ;}}// checks the plug-in function hasIEPlugin (name) {try {new ActiveXObject (name); return true;} in IE ;} catch (ex) {return false ;}} function hasFlash () {var result = hasPlugin ("Flash"); if (! Result) {result = hasIEPlugin ("ShockwaveFlash. ShockwaveFlash");} return result;} console. log (hasFlash (); // true

User Agent Detection

The most important role of a navigator object is to use useragent for user proxy detection. User proxy detection is a last resort, with priority following the capability detection described earlier

Development History

1. In 1993, the NCSA National Supercomputer Center released the world's first web browser Mosaic. The user proxy string of this browser is Mosaic/0.9.

2. Netscape entered the browser development field and named its product code as the abbreviation of Mozilla (Mosaic Killer). The user agent string format is Mozilla/version [language] (platform; encryption type)

3. When Internet Explorer (IE) was released to a widely recognized web browser, Netscape occupied an absolute market share. In order to enable the server to detect Internet Explorer, IE modifies the user agent string to a form compatible with Netscape: Mozilla/2.0 (compatible; MSIE version; Operating System)

4. Various browsers are emerging one after another, and the display format of user proxy strings is getting more and more similar ......

The HTTP specification clearly stipulates that the browser should send a brief user proxy string, specifying the browser name and version number. But in reality it is not so simple. The detection results of various browsers are as follows:

Detection Result

[IE3]

Mozilla/2.0 (compatible; MSIE3.02; windows 95)

[IE6]

Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)

[IE7]

Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)

[IE8]

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)

[IE9]

Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)

[IE10]

Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)

[IE11]

Mozilla/5.0 (MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2 ;. net clr 2.0.50727 ;. net clr 3.5.30729 ;. net clr 3.0.30729 ;. NET4.0C ;. NET4.0E; InfoPath.3; GWX: QUALIFIED; rv: 11.0) like Gecko

[Chrome]

Mozilla/5.0 (Windows NT 6.1; WOW64) G AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36

[Safari]

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2

Firefox]

Mozilla/5.0 (Windows NT 6.1; WOW64; rv: 40.0) Gecko/20100101 Firefox/40.0

[Opera]

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36 OPR/32.0.1948.25

[Ipad]

Mozilla/5.0 (iPad; cpu OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53

[Iphone]

Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4

[Android]

Mozilla/5.0 (Linux; Android 4.2.2; GT-I9505 Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36

Recognize the kernel

Common kernels include Trident, Gecko, and Webkit.

[Note] Because the word like Gecko may appear in the user proxy strings of Trident and Webkit, We will test Gecko again.

Function whichEngine () {var ua = navigator. userAgent; // Trident kernel if (/Trident /. test (ua) {return "Trident";} // Webkit kernel if (/WebKit /. test (ua) {return "WebKit";} // Gecko kernel if (/Gecko /. test (ua) {return "Gecko" ;}} console. log (whichEngine (); // displays "Trident" under IE11"

Recognize browsers

[1] IE

IE3-IE10 can be determined by the MSIE version number, because some IE11 does not appear MSIE characters, and safari also has rv field, so IE11 needs to be determined by the version number and Trident after rv

Function isIE () {var ua = navigator. userAgent; // Trident detection engine, IE8 + if (/Trident /. test (ua) {// IE11 + if (/rv :( \ d + )/. test (ua) {return RegExp ["$1"];} // IE8-IE10 if (/MSIE (\ d + )/. test (ua) {return RegExp ["$1"] ;}}// check the ie id, IE7-if (/MSIE (\ d + )/. test (ua) {return RegExp ["$1"] ;}} console. log (isIE (); // only IE returns the version number, and undefined is returned by other browsers.

[2] chrome

Function isChrome () {var ua = navigator. userAgent; // exclude opera first, Because opera only adds its own Identifier if (! /OPR /. test (ua) {if (/Chrome \/(\ S + )/. test (ua) {return RegExp ["$1"] ;}} console. log (isChrome (); // only Chrome returns version 45.0.2454.93. other browsers return undefined.

[3] safari

Function isSafari () {var ua = navigator. userAgent; // exclude opera if (! /OPR /. test (ua) {// detects chrome and safari if (/Safari /. test (ua) {// detects safari if (/Version \/(\ S + )/. test (ua) {return RegExp ["$1"] ;}}} console. log (isSafari (); // only safari returns version 5.1.7 and undefined in other browsers.

[4] firefox

Function isFireFox () {if (/Firefox \/(\ S + )/. test (navigator. userAgent) {return RegExp ["$1"] ;}} console. log (isFireFox (); // only firefox returns version 40.0, and other browsers return undefined

[5] opera

Function isOpera () {if (/OPR \/(\ S + )/. test (navigator. userAgent) {return RegExp ["$1"] ;}} console. log (isOpera (); // only opera returns version 32.0.1948.25, and undefined is returned by other browsers.

Recognize Operating Systems

Use navigator. platform is easier to detect operating systems because it may include "Win32", "Win64", "MacPPC", "MacIntel", "X11", and "Linux i686, it is consistent in different browsers.

The detailed information of the window system can be obtained through navigator. userAgent.

Windows version-> kernel version Windows XP-> 5.1 Windows Vista-> 6.0 Windows 7-> 6.1 Windows 8-> 6.2 Windows 8.1-> 6.3 Windows 10 technical preview-> 6.4 Windows 10.0 -> 10.0
function whichSyStem(){ var ua = navigator.userAgent; var pf = navigator.platform; if(/Mac/.test(pf)){  return "Mac"; } if(/X11/.test(pf) || /Linux/.test(pf)){  return "Linux"; } if(/Win/.test(pf)){  if(/Windows NT (\d+\.\d+)/.test(ua)){   switch(RegExp["$1"]){    case "5.0":     return "Windows 2000";    case "5.1":     return "Windows XP";    case "6.0":     return "Windows Vista";    case "6.1":     return "Windows 7";    case "6.2":     return "Windows 8";    case "6.3":     return "Windows 8.1";    case "6.4":    case "10.0":     return "Windows 10";        }  } }}console.log(whichSyStem())//Windows 10

Recognize mobile terminals

function whichMobile(){ var ua = navigator.userAgent; if(/iPhone OS (\d+_\d+)/.test(ua)){  return 'iPhone' + RegExp.$1.replace("_","."); } if(/iPad.+OS (\d+_\d+)/.test(ua)){  return 'iPad' + RegExp.$1.replace("_",".") } if(/Android (\d+\.\d+)/.test(ua)){  return 'Android' + RegExp["$1"]; }}console.log(whichMobile())//Android 5.1

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.