1 /** 2 * 获取浏览器类型 3 * 4 * @param getVersion 5 * @returns {*} 6 */ 7 function getBrowser(getVersion) { 8 var ua_str = navigator.userAgent.toLowerCase(), 9 ie_Tridents, 10 trident, 11 match_str, 12 ie_aer_rv, 13 browser_chi_Type; 14 if ("ActiveXObject" in self) { 15 ie_aer_rv = (match_str = ua_str.match(/msie ([d.]+)/)) ? match_str[1] : (match_str = ua_str.match(/rv:([d.]+)/)) ? match_str[1] : 0; 16 ie_Tridents = {"trident/7.0": 11, "trident/6.0": 10, "trident/5.0": 9, "trident/4.0": 8}; 17 trident = (match_str = ua_str.match(/(trident/[d.]+|edge/[d.]+)/)) ? match_str[1] : undefined; 18 browser_chi_Type = (ie_Tridents[trident] || ie_aer_rv) > 0 ? "ie" : undefined; 19 } else { 20 browser_chi_Type = (match_str = ua_str.match(/edge/([d.]+)/)) ? "edge" : 21 (match_str = ua_str.match(/firefox/([d.]+)/)) ? "firefox" : 22 (match_str = ua_str.match(/chrome/([d.]+)/)) ? "chrome" : 23 (match_str = ua_str.match(/opera.([d.]+)/)) ? "opera" : 24 (match_str = ua_str.match(/version/([d.]+).*safari/)) ? "safari" : undefined; 25 } 26 27 var verNum, verStr; 28 verNum = trident && ie_Tridents[trident] ? ie_Tridents[trident] : match_str[1]; 29 30 verStr = (getVersion != undefined) ? browser_chi_Type + "/" + verNum : browser_chi_Type; 31 console.log("浏览器是:" + verStr); 32 return verStr; 33 }
如:
谷歌浏览器
360浏览器(极速模式)
火狐浏览器
搜狗浏览器(极速模式)
IE浏览器
参考文章:https://blog.csdn.net/qq32933432/article/details/103093675