Javascript實現檢測用戶端類型代碼封包_javascript技巧

來源:互聯網
上載者:User

檢測訪問網頁的瀏覽器呈現引擎、平台、Windows作業系統、行動裝置和遊戲系統

/** * Author: laixiangran. * Created by laixiangran on 2015/12/02. * 檢測訪問網頁的瀏覽器呈現引擎、平台、Windows作業系統、行動裝置和遊戲系統 * ******************************************************************** * 各版本瀏覽器在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 (Windows 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 = {};  //呈現引擎資訊  var engine = {    //呈現引擎    ie: 0,    gecko: 0,    webkit: 0,    khtml: 0,    opera:0,    //具體版本號碼    ver: null  };  var browser = {    //瀏覽器    ie: 0,    edge: 0,    firefox: 0,    safari: 0,    konq: 0,    opera: 0,    chrome: 0,    //具體版本號碼    ver: null  };  //平台、裝置和作業系統  var system = {    win: false,    mac: false,    unix: false,    //行動裝置    iphone: false,    ipod: false,    ipad: false,    ios: false,    android: false,    nokiaN: false,    winMobile: false,    //遊戲系統    wii: false, //任天堂    ps: false  //Playstation3  };  //擷取瀏覽器的使用者代理程式字串  var ua = window.navigator.userAgent;  //檢測呈現引擎和瀏覽器  //檢測Presto核心的Opera瀏覽器  if(window.opera){    engine.ver = browser.ver = window.opera.version();    engine.opera = browser.opera = parseFloat(engine.ver);  }  //檢測WebKit 用代理字串中的"AppleWebKit"進行檢測  else if(/AppleWebKit\/(\S+)/.test(ua)){    engine.ver = RegExp["$1"];    engine.webkit = parseFloat(engine.ver);    //確定Microsoft Edge    if(/Edge\/(\S+)/.test(ua)){      browser.ver = RegExp["$1"];      browser.edge = parseFloat(browser.ver);    }    //確定WebKit核心Opera    else if(/OPR\/(\S+)/.test(ua)){      browser.ver = RegExp["$1"];      browser.opera = parseFloat(browser.ver);    }    //確定Chrome    else if(/Chrome\/(\S+)/.test(ua)){      browser.ver = RegExp["$1"];      browser.chrome = parseFloat(browser.ver);    }    //確定Safari    else if(/Version\/(\S+)/.test(ua)){      browser.ver = RegExp["$1"];      browser.safari = parseFloat(browser.ver);    }else{      //近似的確定版本號碼      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 用於Konqueror3.1及更早版本中不包含KHTML的版本,故而就要使用Konqueror的版本來代替  else if(/KHTML\/(\S+)/.test(ua) || /Konqueror\/(\S+)/.test(ua)){    engine.ver = browser.ver = RegExp["$1"];    engine.khtml = browser.konq = parseFloat(engine.ver);s  }  //檢測Gecko 其版本號碼在字串"rv:"的後面  else if(/rv:([^\)]+)\) Gecko\/\d{8}/.test(ua)){    engine.ver = RegExp["$1"];    engine.gecko = parseFloat(engine.ver);    //確定Firefox    if(/Firefox\/(\S+)/.test(ua)){      browser.ver = RegExp["$1"];      browser.firefox = parseFloat(browser.ver);    }  }  //檢測IE  else if(/MSIE ([^;]+)/.test(ua) || /rv:([^\)]+)\) like Gecko/.test(ua)){    engine.ver = browser.ver = RegExp["$1"];    engine.ie = browser.ie = parseFloat(engine.ver);  }  //擷取平台或者作業系統資訊,可能的值:win32、win64、MacPPC、MacIntel、Xll、Linux i686  var p = window.navigator.platform;  //檢測平台  system.win = p.indexOf("Win") == 0;  system.mac = p.indexOf("Mac") == 0;  system.unix = (p == "Xll'") || (p.indexOf("Linux") == 0);  //檢測Windows作業系統  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;        }      }    }  }  //行動裝置  system.iphone = ua.indexOf("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"]);    }  }  //檢測iOS版本  if(system.mac && ua.indexOf("Mobile") > -1){    if(/CPU (?:iPhone )?OS (\d+.\d+)/.test(ua)){      system.ios = parseFloat(RegExp["$1"].replace("_","."));    }else{      system.ios = 2; //不能真正檢測出來,所以只能猜測    }  }  //檢測安卓版本  if(/Android (\d+.\d+)/.test(ua)){    system.android = parseFloat(RegExp["$1"]);  }  //檢測遊戲系統  system.wii = ua.indexOf("wii") > -1;  system.ps = /playstation/i.test(ua);  window.iClient.engine = engine;  window.iClient.browser = browser;  window.iClient.system = system;})();

再給大家一個簡單點的執行個體:判斷windows、linux、android

<html><head><title>判斷作業系統</title><script type="text/javascript">function detectOS() { var sUserAgent = navigator.userAgent; var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows"); var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel"); if (isMac) return "Mac"; var isUnix = (navigator.platform == "X11") && !isWin && !isMac; if (isUnix) return "Unix"; var isLinux = (String(navigator.platform).indexOf("Linux") > -1); var bIsAndroid = sUserAgent.toLowerCase().match(/android/i) == "android";if (isLinux) {if(bIsAndroid) return "Android";else return "Linux"; }if (isWin) { var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1; if (isWin2K) return "Win2000"; var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1; if (isWinXP) return "WinXP"; var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1; if (isWin2003) return "Win2003"; var isWinVista= sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1; if (isWinVista) return "WinVista"; var isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1; if (isWin7) return "Win7"; } return "other"; } document.writeln("您的作業系統是:" + detectOS()); alert(detectOS());</script></head><body></body></html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.