1. 程式人生 > >javascript檢測各種瀏覽器型號和版本、檢測是否支援flash並顯示版本

javascript檢測各種瀏覽器型號和版本、檢測是否支援flash並顯示版本

對於前端來說瀏覽器的型號版本,各個瀏覽器的相容是一個不可忽視的問題。下面本人詳細羅列了測試方法。請複製程式碼新建html檔案複製後訪問。一目瞭然。

最後富有羅列的一些各個不同瀏覽器的版本號,用於資料參考。

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>關於瀏覽器和flash版本的問題</title>
		<style type="text/css">
			#cc,#dd{
				display: none;
			}
		</style>
	</head>
	<body>
		<h3 id="version"></h3>
		<h4>是否為IE10.0以下瀏覽器 或 11.0相容模式: <span id="ie10"></span></h4>
		<h4>是否為IE11.0以上瀏覽器(Edge) : <span id="ie11"></span></h4>
		<h4>是否為Fire Fox瀏覽器 : <span id="firefox"></span></h4>
		<h4>是否為Chrome瀏覽器 : <span id="chrome"></span></h4>
		<h4>是否為Safari瀏覽器 : <span id="safari"></span></h4>
		<h4>是否為Opera瀏覽器 : <span id="opera"></span></h4><br />
		<h4>是否為支援flash : <span id="flash"></span></h4>
		<h4 id="cc">flash版本:<span id="flashversion"></span></h4><br />
		<h4 id="dd">當前IE版本:<span id="ieVersion"></span></h4>
	</body>
	<script type="text/javascript">
		var userAgent = navigator.userAgent; //取得瀏覽器的userAgent字串
		document.getElementById("version").innerHTML = userAgent;
		
		var isOpera = userAgent.indexOf("Opera") > -1 || userAgent.indexOf("OPR/") > -1; //判斷是否Opera瀏覽器
		var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判斷是否IE瀏覽器
		var isEdge = userAgent.indexOf("Edge") > -1 || userAgent.indexOf(") like Gecko")>-1; //判斷是否IE的Edge瀏覽器
		var isFF = userAgent.indexOf("Firefox") > -1; //判斷是否Firefox瀏覽器
		var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1; //判斷是否Safari瀏覽器
		var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1 && userAgent.indexOf("OPR/") == -1 && userAgent.indexOf("Edge") == -1; //判斷Chrome瀏覽器
		
		//獲取IE版本號
		if(isIE){
			var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
			reIE.test(userAgent);
			var fIEVersion = parseFloat(RegExp["$1"]);	//IE版本號
			document.getElementById("dd").style.display = "block";
			document.getElementById("ieVersion").innerHTML = fIEVersion;
		}
		
		document.getElementById("ie10").innerHTML = isIE;
		document.getElementById("ie11").innerHTML = isEdge;
		document.getElementById("chrome").innerHTML = isChrome;
		document.getElementById("opera").innerHTML = isOpera;
		document.getElementById("firefox").innerHTML = isFF;
		document.getElementById("safari").innerHTML = isSafari;
		
		//判斷是否存在flash外掛
		var myFlash = (function(){
		  if(typeof window.ActiveXObject != "undefined"){
		    return new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
		  }else{
		    return navigator.plugins['Shockwave Flash'];
		  }
		})();
		if(myFlash){
			document.getElementById("flash").innerHTML = "true";
		}else{
			document.getElementById("flash").innerHTML = "false";
		}
		
		//返回flash版本號
		(function(){ 
	        var getFlashVersion = function() { 
	        try { 
	            if(typeof window.ActiveXObject != 'undefined') {
	               return parseInt((new ActiveXObject('ShockwaveFlash.ShockwaveFlash')).GetVariable("$version").split(" ")[1].split(",")[0], 10); 
	             }else{
	              return parseInt(navigator.plugins["Shockwave Flash"].description.split(' ')[2], 10);
	             }
	          } catch(e){
	            return 0;
	          }
	        };
	        if(getFlashVersion()){
	        	document.getElementById("cc").style.display = "block"
	        	document.getElementById("flashversion").innerHTML = getFlashVersion();
	        }
//	        console.log(getFlashVersion());
	      })();
		
	</script>
</html>
IE 瀏覽器
IE:7.0
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
IE:8.0
Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)
IE :9.0
Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E) IE:10.0 Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E) IE:11.0
Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; rv:11.0) like Gecko Chome瀏覽器 Chrome:版本 60.0.3112.78(正式版本)(64 位) Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36 Chrome:版本 60.0.3112.101(正式版本)(64位) Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36 Chrome:版本 58.0.3015.0 (64-bit) Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3015.0 Safari/537.36 Fire Fox Fire fox:54.0 Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0 Safari(IE) Safari:5.1.7(7534.57.2) Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2 QQ瀏覽器 極速模式 Chrome53.版本 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2141.400 QQBrowser/9.5.10219.400 相容模式 Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Core/1.53.2141.400 QQBrowser/9.5.10219.400; rv:11.0) like Gecko Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Core/1.53.2141.400 QQBrowser/9.5.10219.400) Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Core/1.53.2141.400 QQBrowser/9.5.10219.400) Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Core/1.53.2141.400 QQBrowser/9.5.10219.400) Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Core/1.53.2141.400 QQBrowser/9.5.10219.400) 360瀏覽器 極速模式 Chrome45.版本 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36 相容模式 Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0) Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0) Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E) Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)(此版本為IE7和IE11相容檢視模式) Opera瀏覽器 Opera:47.0.2631.55 (PGO) - Opera Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.78 Safari/537.36 OPR/47.0.2631.55 EDGE(win 10) Edge: Mozilla/5.0 (Window NT 10.0; Win64;x64)AppleWebKit/537.36(KHTML,like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393