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

    原文作者:蕭朙偉
    原文地址: https://blog.csdn.net/sanai_1992/article/details/77508651
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞