The user agent string userAgent can implement four identification functions: String useragent

Source: Internet
Author: User

The user agent string userAgent can implement four identification functions: String useragent


User proxy string: navigator. userAgent

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 that simple.

Development History

[1] in 1993, the NCSA National Supercomputer Center released Mosaic, the world's first web browser. 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] Internet Explorer 3, the first web browser that has won wide user recognition, was released by IE. At that time, Netscap had 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 emerged one after another, and the display format of user proxy strings became more and more similar ......

Test Tool

Use various desktop browser debugging tools, mainly IE debugging tools and chrome emulation mobile phone debugging tools


Desktop test results

[1] IE

[1, 1.1] IE3

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

[1, 1.2] IE6

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

[1, 1.3] IE7

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

[1, 1.4] IE8

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

[1, 1.5] IE9

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

[1, 1.6] IE10

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

[1, 1.7] 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

[2] chrome

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

[3] safari

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

[4] firefox

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

[5] 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

Mobile testing results

[1] 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

[2] 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

[3] 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

Identify the browser 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"

Identify the browser version

[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> kernel
Windows XP-> 5.1
Windows Vista-> 6.0
Windows 7-> 6.1
Windows 8-> 6.2
Windows 8.1-> 6.3
Windows 10 technology preview-> 6.4
Windows 10 (Build 9880 +)-> 10

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":     return "Windows 10";        }  } }}console.log(whichSyStem())//Windows 7

Identify mobile devices

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

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: 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.