JS gets the current operating system type, browser, terminal type tools

Source: Internet
Author: User

Summarizes a JavaScript to get the current terminal type (PC, mobile), operating system type, browser type, browser version of the gadget.

Personally feel OK, tested no problem, can identify IE7 and above.

/**author:weihuandate:2017-6-26**/var _agentinfo = {devicetype: "",//PC or Mobileosname: "",//Windows, Android, Linu X and so On...browsername: "",//Chrome, Safari, Firefox, IE and so on...browserver: "",//browser version, Importan T if in IE environment.adapttype:0,//A type value, Adapt to the screens due to width_init:function () {_agentinfo.setdevic Eandos (); _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 was 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 was 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 was after "chrome" Browsername = "Chrome "; Fullversion = nagt.substring (veroffset+7);} else if ((Veroffset=nagt.indexof ("Safari"))!=-1) {//In Safari, the true version was after "Safari" or after "version" b Rowsername = "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 was after "Firefox" browsername = "F Irefox "; Fullversion = nagt.substring (veroffset+8);} else if ((Nameoffset=nagt.lastindexof (') +1) < (Veroffset=nagt.lastindexof ('/'))} {//In the most other browsers, "Nam E/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=fullver Sion.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 screens 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 gets the current operating system type, browser, terminal type tools

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.