Home>

User agent string:navigator.useragent, this article shares the four recognitions of user agent string useragent,

definition

User agent string:navigator.useragent

The http specification clearly states thatThe browser should send a short user agent string,Indicate the name and version number of the browser.But in reality it is not so simple.

Development History

[1] In 1993, the NCSA National Supercomputer Center in the United States released the world's first web browser, mosaic, whose user-agent string was mosaic/0.9

[2] Netscape enters the field of browser development,Name the code of your product as short for mozilla (mosaic killer), and the user agent string format is mozilla/version number [language] (platform;encryption type)

[3] ie3, the first web browser released by ie, has won wide recognition from users. At that time, netscap had occupied absolute market share.In order to enable the server to detect ie, ie changed the user agent string to a netscape-compatible form:mozilla/2.0 (compatible;msie version number;operating system)

[4] Browsers appear one after another,The display format of user agent strings is becoming more and more similar ...

test tools

Using various desktop browser debugging tools,Mainly ie debugging tools and emulation mobile phone debugging tools for chrome

Desktop test results

[1] ie

[1.1] ie3

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

[1.2] ie6

Mozilla/4.0 (compatible;msie 6.0;windows nt 5.1)

[1.3] ie7

Mozilla/4.0 (compatible;msie 7.0;windows nt 6.0)

[1.4] ie8

Mozilla/4.0 (compatible;msie 8.0;windows nt 6.1;trident/4.0)

[1.5] ie9

Mozilla/5.0 (compatible;msie 9.0;windows nt 6.1;trident/5.0)

[1.6] ie10

Mozilla/5.0 (compatible;msie 10.0;windows nt 6.2;trident/6.0)

[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 test 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 are trident, gecko and webkit

[Note] Because gecko-like words may appear in the user agent strings of trident and webkit, so gecko

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 ());//ie11 shows "trident"

Identify browser version

[1] ie

Ie3-ie10 can be judged by the version number of msie,Because some ie11 does not have msie characters, and there is also a rv field in safari, ie11 needs to use the version number and trident after rv to cooperate with the judgment.

function isie () {
 var ua=navigator.useragent;
 //Detect trident 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"];
  }
 }
 //Detect ie logo, ie7-
 if (/msie (\ d +) /. test (ua)) {
  return regexp ["$1"];
 }
}
console.log (isie ());//Only ie will return the version number,Other browsers return undefined
</pre>
</div>
<p>
<strong>
[2] chrome
</strong>
</p>
<div>
<pre>
function ischrome () {
 var ua=navigator.useragent;
 //Exclude opera first, because opera just added its own logo after chrome's useragent
 if (!/opr/.test (ua)) {
  if (/chrome \/(\ s +) /. test (ua)) {
   return regexp ["$1"];
  }
 }
}
console.log (ischrome ());//Only chrome will return the version number 45.0.2454.93, other browsers will return undefined
</pre>
</div>
<p>
<strong>
[3] safari
</strong>
</p>
<div>
<pre>
function issafari () {
 var ua=navigator.useragent;
 //Exclude opera first
 if (!/opr/.test (ua)) {
  //Detected chrome and safari browsers
  if (/safari/.test (ua)) {
   //Safari detected
   if (/version \/(\ s +) /. test (ua)) {
    return regexp ["$1"];
   }
  }
 }
}
console.log (issafari ());//Only safari will return version number 5.1.7, other browsers return undefined
</pre>
</div>
<p>
<strong>
[4] firefox
</strong>
</p>
<div>
<pre>
function isfirefox () {
 if (/firefox \/(\ s +) /. test (navigator.useragent)) {
  return regexp ["$1"];
 }
}
console.log (isfirefox ());//Only firefox will return version number 40.0, other browsers will return undefined

[5] opera

function isopera () {
 if (/opr \/(\ s +) /. test (navigator.useragent)) {
  return regexp ["$1"];
 }
}
console.log (isopera ());//Only opera will return the version number 32.0.1948.25, other browsers return undefined

Identifying the operating system

Using navigator.platform to detect the operating system is simpler,Because it may include the values ​​"win32", "win64", "macppc", "macintel", "x11", and "linux i686", etc., and are consistent in different browsers.

Navigator.useragent can get detailed information about the window system.

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

Identifying 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
  • Previous Simple code example for prime and greatest common divisor in Java
  • Next Batch processing to filter duplicate rows