js judge browser information

js judges whether it is Google Chrome through the browser

// test() 方法用于检测一个字符串是否匹配某个模式.
let isChrome = /(Chrome|Chromium|AppleWebKit)/i.test(navigator.userAgent)

Determine whether it is windows or safari

let isMac = /macintosh|mac os x/i.test(navigator.userAgent);
let isWin32 = /windows|win32/i.test(navigator.userAgent);//Windows
let isWin64 = /windows|win64|wow64/i.test(navigator.userAgent);//Windows

Determine the operating system

var sUserAgent = navigator.userAgent.toLowerCase();   
						
						var isWin = (navigator.platform == "Win32") || (navigator.platform == "Win64")|| (navigator.platform == "wow64");   
						
						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  
							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";   
							var isWin8 = sUserAgent.indexOf("windows nt 6.2") > -1 || sUserAgent.indexOf("Windows 8") > -1;  
							if (isWin8) return "Win8"; 
							var isWin10 = sUserAgent.indexOf("windows nt 10.0")>-1||sUserAgent.indexOf("Windows 10")>-1;	
							if(isWin10)return "Win10";				
						}  
						return "其他";

Determine the version number of the current operating system (win32\win64)

var sUserAgent = navigator.userAgent.toLowerCase();   
var is64 = sUserAgent.indexOf("win64") > -1||sUserAgent.indexOf("wow64") > -1;  
if (is64) {
    
      
	return "64位";  
}else{
    
      
   return "32位";    
}

Determine browser type

var rMsie = /(msie\s|trident\/7)([\w\.]+)/;  
					  var rTrident = /(trident)\/([\w.]+)/;  
					  var rEdge = /(chrome)\/([\w.]+)/;//IE
					  
					  var rFirefox = /(firefox)\/([\w.]+)/;  //火狐
					  var rOpera = /(opera).+version\/([\w.]+)/;  //旧Opera
					  var rNewOpera = /(opr)\/(.+)/;  //新Opera 基于谷歌
					  var rChrome = /(chrome)\/([\w.]+)/; //谷歌 
					  var rUC = /(chrome)\/([\w.]+)/;//UC
					  var rMaxthon = /(chrome)\/([\w.]+)/;//遨游
					  var r2345 =  /(chrome)\/([\w.]+)/;//2345
					  var rQQ =  /(chrome)\/([\w.]+)/;//QQ
					  //var rMetasr =  /(metasr)\/([\w.]+)/;//搜狗
					  var rSafari = /version\/([\w.]+).*(safari)/;
					  
					  var ua = navigator.userAgent.toLowerCase();  
 
 
					  var matchBS, matchBS2;  
					  
					  //IE 低版
					  // exec() 方法用于检索字符串中的正则表达式的匹配。
					  matchBS = rMsie.exec(ua);  
					  if (matchBS != null) {
    
      
						  matchBS2 = rTrident.exec(ua);  
						  if (matchBS2 != null) {
    
      
							  switch (matchBS2[2]) {
    
      
							  case "4.0":  
								  return {
    
      
									  browser:  
									  "Microsoft IE",  
									  version: "IE: 8"  //内核版本号
								  };  
								  break;  
							  case "5.0":  
								  return {
    
      
									  browser:  
									  "Microsoft IE",  
									  version: "IE: 9"  
								  };  
								  break;  
							  case "6.0":  
								  return {
    
      
									  browser:  
									  "Microsoft IE",  
									  version: "IE: 10"  
								  };  
								  break;  
							  case "7.0":  
								  return {
    
      
									  browser:  
									  "Microsoft IE",  
									  version: "IE: 11"  
								  };  
								  break;  
							  default:  
								  return {
    
      
									  browser:  
									  "Microsoft IE",  
									  version: "Undefined"  
								  };  
							  }  
						  } else {
    
      
							  return {
    
      
								  browser: "Microsoft IE",  
								  version: "IE:"+matchBS[2] || "0"  
							  };  
						  }  
					  }  
					  //IE最新版
					   matchBS = rEdge.exec(ua);  	 				   
					  if ((matchBS != null) && (!(window.attachEvent))) {
    
      
						  return {
    
      
							  browser: "Microsoft Edge",  
							  version: "Chrome/"+matchBS[2] || "0"  
						  };  
					  }  
                                          //UC浏览器					  
					  matchBS = rUC.exec(ua);                         		   
					  if ((matchBS != null) && (!(window.attachEvent))) {
    
      
						  return {
    
      
							  browser: "UC",  
							  version: "Chrome/"+matchBS[2] || "0"  
						  };  
					  }  
					  //火狐浏览器
					  matchBS = rFirefox.exec(ua);  
					  if ((matchBS != null) && (!(window.attachEvent))) {
    
      
						  return {
    
      
							  browser: "火狐",  
							  version: "Firefox/"+matchBS[2] || "0"  
						  };  
					  }  
                                         //Oper浏览器					 
					 matchBS = rOpera.exec(ua);  
					  if ((matchBS != null) && (!(window.attachEvent))) {
    
      
						  return {
    
      
							  browser: "Opera",  
							  version: "Chrome/"+matchBS[2] || "0"  
						  };  
					  }  
					  //遨游
					   matchBS = rMaxthon.exec(ua);  	 				   
					  if ((matchBS != null) && (!(window.attachEvent))) {
    
      
						  return {
    
      
							  browser: "遨游",  
							  version: "Chrome/"+matchBS[2] || "0"  
						  };  
					  }  
                                          //2345浏览器					  
 					  matchBS = r2345.exec(ua);  	 				   
					  if ((matchBS != null) && (!(window.attachEvent))) {
    
      
						  return {
    
      
							  browser: "2345",  
							  version: "Chrome/ "+matchBS[2] || "0"  
						  };  
					  }  
                                          //QQ浏览器					  
					  matchBS = rQQ.exec(ua);  	 				   
					  if ((matchBS != null) && (!(window.attachEvent))) {
    
      
						  return {
    
      
							  browser: "QQ",  
							  version: "Chrome/"+matchBS[2] || "0"  
						  };  
					  }  
					  //Safari(苹果)浏览器
					  matchBS = rSafari.exec(ua);  
					  if ((matchBS != null) && (!(window.attachEvent)) && (!(window.chrome)) && (!(window.opera))) {
    
      
						  return {
    
      
							  browser: "Safari",  
							  version: "Safari/"+matchBS[1] || "0"  
						  };  
					  }  
					  //谷歌浏览器
					   matchBS = rChrome.exec(ua);  
					  if ((matchBS != null) && (!(window.attachEvent))) {
    
      
						  matchBS2 = rNewOpera.exec(ua);  
						  if (matchBS2 == null) {
    
      
							  return {
    
      
								  browser: "谷歌",  
								  version: "Chrome/"+matchBS[2] || "0"  
							  };  
						  } else {
    
      
							  return {
    
      
								  browser: "Opera",  
								  version: "opr/"+matchBS2[2] || "0"  
							  };  
						  }  
					  }  
				  }

Guess you like

Origin blog.csdn.net/qq_34164814/article/details/103819354