如何判斷瀏覽器JS代碼
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
window.onload = function(){
//測試媒體類型
let checkMimeType = function(option, value){
var mimeTypes = navigator.mimeTypes;
for (var mt in mimeTypes) {
if (mimeTypes[mt][option] == value) {
return true;
}
}
return false;
}
let judge = function(){
let agent = navigator.userAgent;
// alert(agent);
let ie = /Trident\/(\d+\.)+\d+/ig;
let ff = /Firefox\/(\d+\.)+\d+/ig;
let chrome = /Chrome\/(\d+\.)+\d+/ig;//谷歌,360
let saf = /Safari\/(\d+\.)+\d+/ig;//蘋果
let opr = /OPR\/(\d+\.)+\d+/ig;
let is360 = checkMimeType("type", "application/vnd.chromium.remoting-viewer");
//如果存在Firefox,就返回與之匹配的正則
if(agent.match(ff)){
return "火狐--" + agent.match(ff);
}else if(agent.match(ie)){
return "IE--" + agent.match(ie);
}else if(agent.match(saf) && !agent.match(chrome)){//找到蘋果,但找不到谷歌
return "蘋果--" + agent.match(saf) ;
}else if(agent.match(chrome)){//有谷歌
if(agent.match(opr)){//判斷是否是歐鵬
return "歐鵬--" + agent.match(opr);
}else if(is360){//判斷是否是360
return "360--" + agent.match(chrome);
}
return "谷歌--" + agent.match(chrome);
}
}
let browser = judge();
alert(browser);
//只看版本
// let verinfo = (browser+"").replace(/[^\d.]/ig,""); //用空去替代返回的字符串出現的非數字
// alert(verinfo);
}
</script>
</body>
</html>
非常感謝……ZJ
如何判斷瀏覽器JS代碼