JS 擷取當前作業系統類型,瀏覽器,終端類型的工具

來源:互聯網
上載者:User

標籤:ipa   int   dev   dap   dos   ant   offset   ie7   ret   

總結了一個JavaScript擷取當前終端類型(pc, mobile),作業系統類型,瀏覽器類型,瀏覽器版本的小工具。

個人覺得還行,測試過沒有問題,能識別ie7以及以上。

/**author: Weihuandate: 2017-6-26**/var _AgentInfo = {deviceType: "",  // pc or mobileOSname: "",// windows, Android, linux and so on...browserName: "", //  chrome, safari, firefox, IE and so on...browserVer: "",   //  browser version, important if in IE environment.adaptType: 0,// A type value, Adapt to the screen due to width_init: function(){_AgentInfo.setDeviceAndOS();_AgentInfo.setBrowser();},setDeviceAndOS: function(){var name = "unknown";if(window.navigator.userAgent.indexOf("Android") != -1){name = "Android";}else if(window.navigator.userAgent.indexOf("iPhone") != -1){name = "iPhone";}else if(window.navigator.userAgent.indexOf("SymbianOS") != -1){name = "SymbianOS";}else if(window.navigator.userAgent.indexOf("Windows Phone") != -1){name = "Windows Phone";}else if(window.navigator.userAgent.indexOf("iPad") != -1){name = "iPad";}else if(window.navigator.userAgent.indexOf("iPod") != -1){name = "iPod";}if(name != "unknown"){_AgentInfo.OSname = name;_AgentInfo.deviceType = "mobile";return;}if (window.navigator.userAgent.indexOf("Windows NT 10.0")!= -1){name="Windows 10";}else if (window.navigator.userAgent.indexOf("Windows NT 6.2") != -1){ name="Windows 8";}else if (window.navigator.userAgent.indexOf("Windows NT 6.1") != -1){name="Windows 7";}else if (window.navigator.userAgent.indexOf("Windows NT 6.0") != -1){name="Windows Vista";}else if (window.navigator.userAgent.indexOf("Windows NT 5.1") != -1){name="Windows XP";}else if (window.navigator.userAgent.indexOf("Windows NT 5.0") != -1){name="Windows 2000";}else if (window.navigator.userAgent.indexOf("Mac") != -1){name="Mac/iOS";}else if (window.navigator.userAgent.indexOf("X11") != -1){name="UNIX";}else if (window.navigator.userAgent.indexOf("Linux") != -1){name="Linux";}_AgentInfo.OSname = name;_AgentInfo.deviceType = "pc";},setBrowser: function(){var nAgt = navigator.userAgent;var browserName  = navigator.appName;var fullVersion  = ‘‘+parseFloat(navigator.appVersion);var majorVersion = parseInt(navigator.appVersion,10);var nameOffset,verOffset,ix;if ((verOffset=nAgt.indexOf("Opera"))!=-1) { // In Opera, the true version is after "Opera" or after "Version" browserName = "Opera"; fullVersion = nAgt.substring(verOffset+6); if ((verOffset=nAgt.indexOf("Version"))!=-1) fullVersion = nAgt.substring(verOffset+8);}else if ( (nAgt.indexOf("Trident"))!=-1 ) {   // ( ver >= ie7) In MSIE, the true version is after "MSIE" in userAgentif((verOffset=nAgt.indexOf("MSIE"))!=-1){fullVersion = nAgt.substring(verOffset+5);}else {fullVersion = ‘11.0‘;}if(fullVersion == 5){fullVersion = "11.0";}browserName = "IE";}else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {  // In Chrome, the true version is after "Chrome" browserName = "Chrome"; fullVersion = nAgt.substring(verOffset+7);}else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {   // In Safari, the true version is after "Safari" or after "Version" browserName = "Safari"; fullVersion = nAgt.substring(verOffset+7); if ((verOffset=nAgt.indexOf("Version"))!=-1) fullVersion = nAgt.substring(verOffset+8);}else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {    // In Firefox, the true version is after "Firefox" browserName = "Firefox"; fullVersion = nAgt.substring(verOffset+8);}else if ( (nameOffset=nAgt.lastIndexOf(‘ ‘)+1) < (verOffset=nAgt.lastIndexOf(‘/‘)) ){   // In most other browsers, "name/version" is at the end of userAgent browserName = nAgt.substring(nameOffset,verOffset); fullVersion = nAgt.substring(verOffset+1); if (browserName.toLowerCase()==browserName.toUpperCase()) {browserName = navigator.appName; }}if ((ix=fullVersion.indexOf(";"))!=-1)  // trim the fullVersion string at semicolon/space if present fullVersion=fullVersion.substring(0,ix);if ((ix=fullVersion.indexOf(" "))!=-1) fullVersion=fullVersion.substring(0,ix);majorVersion = parseInt(‘‘+fullVersion,10);if (isNaN(majorVersion)) { fullVersion  = ‘‘+parseFloat(navigator.appVersion); majorVersion = parseInt(navigator.appVersion,10);}_AgentInfo.browserName = browserName;_AgentInfo.browserVer = fullVersion;},isMobile: function(){if(_AgentInfo.deviceType == "mobile"){return true;}return false;},setAdaptType(){    // A type value, Adapt to the screen due to width. For convenientif(screen.width <= 374){_AgentInfo.adaptType = 0;}else if(screen.width <= 413){_AgentInfo.adaptType = 1;}else {_AgentInfo.adaptType = 2;}}}_AgentInfo._init();

  

JS 擷取當前作業系統類型,瀏覽器,終端類型的工具

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.