Javascript-based client-type code package _ javascript skills

Source: Internet
Author: User
Tags microsoft edge
In the past, I always thought that using the user proxy string to check the browser is that type. This is really a big mistake, next, let's talk about how to determine the device type used by the current browser through js. How can we detect the browser rendering engine, platform, Windows operating system, mobile devices, and game systems that access the webpage?

/*** Author: laixiangran. * Created by laixiangran on 2015/12/02. * detects browser rendering engines, platforms, Windows operating systems, mobile devices, and game systems that access the Web page ******************** **************************************** * ********* user proxy strings of browsers of different versions under windows10.0: * Google Chrome 45.0.2454.85 -- "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) chrome/45.0.2454.85 Safari/537.36 "* Opera 31.0.1889.174 --" Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36 OPR/31.0.1889.174 "* Microsoft Edge --" Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240 "* Firefox 40.0.3 --" Mozilla/5.0 (Windows NT 10.0; WOW64; rv: 40.0) gecko/20100101 Firefox/40.0 "* Internet Explorer 11 + --" Mozilla/5.0 (W Indows NT 10.0; WOW64; Trident/7.0 ;. NET4.0C ;. NET4.0E ;. net clr 2.0.50727 ;. net clr 3.0.30729 ;. net clr 3.5.30729; rv: 11.0) like Gecko "* Internet Explorer 10---" Mozilla/5.0 (compatible; MSIE x.0; Windows NT 10.0; WOW64; Trident/8.0 ;. NET4.0C ;. NET4.0E ;. net clr 2.0.50727 ;. net clr 3.0.30729 ;. net clr 3.5.30729) "*/(function () {window. iClient ={}; // rendering engine information var engine ={// rendering engine ie: 0, gecko: 0, Webkit: 0, khtml: 0, opera: 0, // specific Version ver: null}; var browser = {// browser ie: 0, edge: 0, firefox: 0, safari: 0, konq: 0, opera: 0, chrome: 0, // The specific Version ver: null }; // platform, device, and operating system var system = {win: false, mac: false, unix: false, // mobile device iphone: false, ipod: false, ipad: false, ios: false, android: false, nokiaN: false, winMobile: false, // game system wii: false, // Nintendo ps: false // Playstation3}; // obtain the browser's User Agent string var ua = window. navigator. userAgent; // detects the rendering engine and browser // detects the operabrowser if (window. opera) {engine. ver = browser. ver = window. opera. version (); engine. opera = browser. opera = parseFloat (engine. ver);} // detects WebKit using the "AppleWebKit" in the proxy string to detect else if (/AppleWebKit \/(\ S + )/. test (ua) {engine. ver = RegExp ["$1"]; engine. webkit = parseFloat (engine. ver); // confirm Microsoft Edge if (/Edge \/(\ S + )/. test (ua) {bro Invalid. ver = RegExp ["$1"]; browser. edge = parseFloat (browser. ver);} // determine the WebKit kernel Opera else if (/OPR \/(\ S + )/. test (ua) {browser. ver = RegExp ["$1"]; browser. opera = parseFloat (browser. ver);} // determine Chrome else if (/Chrome \/(\ S + )/. test (ua) {browser. ver = RegExp ["$1"]; browser. chrome = parseFloat (browser. ver);} // determine the Safari else if (/Version \/(\ S + )/. test (ua) {browser. ver = RegExp ["$1"]; browser. safari = pa RseFloat (browser. ver);} else {// similarly identified version number var safariVersion = 1; if (engine. webkit <100) {safariVersion = 1;} else if (engine. webkit <312) {safariVersion = 1.2;} else if (engine. webkit <412) {safariVersion = 1.3;} else {safariVersion = 2;} browser. ver = browser. safari = safariVersion; }}// KHTML is used for Konqueror3.1 and earlier versions that do not contain KHTML, therefore, we must use the Konqueror version to replace else if (/KHTML \/(\ S + )/. test (ua) |/Konqueror \/ (\ S + )/. test (ua) {engine. ver = browser. ver = RegExp ["$1"]; engine. khtml = browser. konq = parseFloat (engine. ver); s} // check whether the Gecko version is later than the string "rv:" else if (/rv :( [^ \)] + )\) gecko \/\ d {8 }/. test (ua) {engine. ver = RegExp ["$1"]; engine. gecko = parseFloat (engine. ver); // determine Firefox if (/Firefox \/(\ S + )/. test (ua) {browser. ver = RegExp ["$1"]; browser. firefox = parseFloat (browser. ver) ;}}// check IE else if (/MSIE ([^;] + )/. Test (ua) |/rv :( [^ \)] +) \) like Gecko /. test (ua) {engine. ver = browser. ver = RegExp ["$1"]; engine. ie = browser. ie = parseFloat (engine. ver);} // obtain information about the platform or operating system. Possible values: win32, win64, MacPPC, MacIntel, Xll, and Linux i686 var p = window. navigator. platform; // detection platform system. win = p. indexOf ("Win") = 0; system. mac = p. indexOf ("Mac") = 0; system. unix = (p = "Xll '") | (p. indexOf ("Linux") = 0); // checks if (System. win) {if (/Win (?: Dows )? ([^ Do] {2}) \ s? (\ D + \. \ d + )? /. Test (ua) {if (RegExp ["$1"] = "NT") {switch (RegExp ["$2"]) {case "5.0": system. win = "2000"; break; case "5.1": system. win = "XP"; break; case "6.0": system. win = "Vista"; break; case "7": system. win = "7"; break; case "8": system. win = "8"; break; case "8.1": system. win = "8.1"; break; case "10.0": system. win = "10.0"; break; default: system. win = "NT"; break ;}}// mobile device system. iphone = ua. in DexOf ("iPhone")>-1; system. ipod = ua. indexOf ("iPod")>-1; system. ipad = ua. indexOf ("iPad")>-1; system. nokiaN = ua. indexOf ("NokiaN")>-1; // window mobile if (system. win = "CE") {system. winMobile = system. win;} else if (system. win = "Ph") {if (/Windows Phone OS (\ d +. \ d + )/. test (ua) {system. win = "Phone"; system. winMobile = parseFloat (RegExp ["$1"]) ;}// checks iOS if (system. mac & ua. indexOf ("Mob Ile ")>-1) {if (/CPU (?: IPhone )? OS (\ d +. \ d + )/. test (ua) {system. ios = parseFloat (RegExp ["$1"]. replace ("_",". ");} else {system. ios = 2; // cannot be detected, so you can only guess} // detect the Android version if (/Android (\ d +. \ d + )/. test (ua) {system. android = parseFloat (RegExp ["$1"]);} // detects the game system. wii = ua. indexOf ("wii")>-1; system. ps =/playstation/I. test (ua); window. iClient. engine = engine; window. iClient. browser = browser; window. iClient. system = system ;})();

Let's give you another simple example: Judge windows, linux, and android

Operating System judgment
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.