js 获取浏览器类别
程序员文章站
2022-04-24 23:14:29
...
今天花了大半个下午,整理了一下 js 获取 浏览器类别,各浏览器 userAgent、version 截图如下:
具体代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function getBrowserType(){
var browserType = "";
var agent = window.navigator.userAgent;
// ie 7 8 9 10
if(agent.indexOf('Trident/7.0') != -1){
var ieRegExp = new RegExp("(MSIE \\d{1,2}\\.\\d);");
var exec = ieRegExp.exec(agent);
switch(RegExp.$1){
case 'MSIE 7.0':
browserType = "IE7";
break;
case 'MSIE 8.0':
browserType = "IE8";
break;
case 'MSIE 9.0':
browserType = "IE9";
break;
case 'MSIE 10.0':
browserType = "IE10";
break;
}
// ie11
if(agent.indexOf('rv:11.0') != -1){
browserType = "IE11";
}
}
// edge
if(agent.indexOf('Edge') != -1){
browserType = "Edge";
}
// chrome
if(agent.indexOf('Chrome') != -1 && agent.indexOf('Safari') != -1 && agent.indexOf('Edge') == -1){
browserType = "Chrome";
}
// firefox
if(agent.indexOf('Firefox') != -1){
browserType = "Firefox";
}
// safari
if(agent.indexOf('Safari') != -1 && agent.indexOf('Chrome') == -1){
browserType = "Safari";
}
// opera
if(agent.indexOf('OPR') != -1){
browserType = "Opera";
}
alert("browser type:" + browserType);
return browserType;
}
</script>
</head>
<body onload="getBrowserType();">
</body>
</html>