User Agent string useragent Four recognizable _javascript techniques that can be implemented

Source: Internet
Author: User
Tags string format

Defined

User Agent string: navigator.useragent

The HTTP specification specifies that the browser should send a short user agent string indicating the name and version number of the browser. But in reality it is not so simple.

Development history

"1" 1993 U.S. NCSA National Supercomputer Center released the world's first web browser mosaic, the browser's user agent string is mosaic/0.9

"2" Netscape into the field of browser development, the name of their own products named Mozilla (Mosaic Killer) shorthand, user agent string format mozilla/version number [language] (platform; encryption type)

"3" IE released the first to win the user's widely recognized web browser IE3, when Netscap already occupied absolute market share, in order to enable the server to detect Ie,ie to modify the user agent string to be compatible with Netscape form: mozilla/2.0 ( compatible; MSIE version number; operating system)

"4" browsers appear in succession, the user agent string display format is more and more similar ...

Testing tools

Use the desktop browser debugging tools, mainly IE debugging tools and chrome emulation mobile phone Debugging Tools

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

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

"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

"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 the word like gecko may appear in the user agent string for Trident and WebKit, the last Test 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 display "Trident"

Identify browser version

"1" IE

Ie3-ie10 can be judged by the version number of MSIE, because some IE11 do not appear msie characters, and Safari also has the RV field, so IE11 need to pass the RV version number and Trident to match the judgment

function Isie () {
 var ua = navigator.useragent;
 Detecting the Trident engine, ie8+
 if (/trident/.test (UA)) {
  //ie11+
  if (/rv: (\d+)/.test (UA)) {return
   regexp["$"] ;
  } 
  Ie8-ie10 
  if (/msie (\d+)/.test (UA) {return
   regexp["$"];
  }
 Detect IE identification, ie7-
 if (/msie (\d+)/.test (UA)) {return
  regexp["$"];
 }
Console.log (Isie ())//only IE will return the version number, other browsers are returned undefined

"2" Chrome

function Ischrome () {
 var ua = navigator.useragent;
 First of all, opera is excluded, because opera only adds its own logo if (
 !/opr/.test (UA)) {if (
  /chrome\/(\s+)/.test (UA)) {useragent after Chrome. Return
   regexp["$";
}} Console.log (Ischrome ())//Only Chrome will return version number 45.0.2454.93, other browsers will return undefined

"3" Safari

function Issafari () {
 var ua = navigator.useragent;
 First Exclude opera
 if (!/opr/.test (UA)) {/
  /detect chrome and Safari browser
  if (/safari/.test (UA)) {
   //detect Safari
   if (/version\/(\s+)/.test (UA)) {return
    regexp["$"
}}} Console.log (Issafari ());//Only Safari returns version number 5.1.7, and other browsers return undefined

"4" Firefox

function Isfirefox () {
 if (/firefox\/(\s+)/.test (navigator.useragent)) {return
  regexp["$"]
Console.log (Isfirefox ())//Only Firefox will return version number 40.0, other browsers are returned undefined

"5" Opera

function Isopera () {
 if (/opr\/(\s+)/.test (navigator.useragent)) {return
  regexp["$"] 

Identify the operating system

Using Navigator.platform to detect the operating system is simpler because it may include values such as "Win32", "Win64", "MACPPC", "Macintel", "X11" and "Linux i686", and are consistent across browsers.

The details of the window system can be obtained by 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 Technology Preview version-> 6.4
Windows (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["$") {case
    "5.0":
     return "Windows";
    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
    "ten": Return
     "Windows";
}}} Console.log (Whichsystem ())//windows 7

Identify mobile end 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["$"];
 }
Console.log (Whichmobile ())//android 5.1

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.