调用navigator对象的userAgent属性可以获取到浏览器版本,型号,内核,甚至安装的插件等信息。
navigator.userAgent
一些主流浏览器的部分版本userAgent信息如下:
IE7
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)
IE8
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)
IE9
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)
IE11
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
Edge 17
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134
Chrome 69
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36
Firefox 62
Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0
Safari 60
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.1 Safari/605.1.15
Opera 56
Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36 OPR/56.0.3051.88
可以通过ActiveXObject对象来判断当前浏览器是否是IE浏览器。
if(!!window.ActiveXObject || "ActiveXObject" in window){
return true;
}else{
return false;
}
如上述代码所示,只有在IE浏览器下,window.ActiveXObject会返回一个对象,!window.ActiveXObject会返回false,!!window.ActiveXObject则返回true,但是在IE11版本下,window.ActiveXObject会返回undefined,导致前面两个表达式返回的结果正好相反,因此就需要再增加一个判断,“ActiveXObject” in window在IE11版本下返回true,从而使得条件依然满足。 在非IE浏览器下,上述条件将返回false,因为window.ActiveXObject会返回undefined且“ActiveXObject” in window也会返回false。
判断主流浏览器版本的思路其实就是使用navigator.userAgent获取到userAgent信息,然后对userAgent信息使用正则表达式进行匹配判断。话不多说,直接上代码。
function getBrowserInfo() {
var agent = navigator.userAgent.toLowerCase();
var regStr_ie = /msie [\d.]+;/gi;
var regStr_ff = /firefox\/[\d.]+/gi
var regStr_chrome = /chrome\/[\d.]+/gi;
var regStr_saf = /safari\/[\d.]+/gi;
var regStr_opera=/opera\/[\d.]+/gi;
var isIE = agent.indexOf("compatible") > -1 && agent.indexOf("msie" > -1); //判断是否IE<11浏览器
var isEdge = agent.indexOf("edge") > -1 && !isIE; //判断是否IE的Edge浏览器
var isIE11 = agent.indexOf('trident') > -1 && agent.indexOf("rv:11.0") > -1;
if (isIE) {
var reIE = new RegExp("msie (\\d+\\.\\d+);");
reIE.test(agent);
var fIEVersion = parseFloat(RegExp["$1"]);
if (fIEVersion == 7) {
return "IE/7";
} else if (fIEVersion == 8) {
return "IE/8";
} else if (fIEVersion == 9) {
return "IE/9";
} else if (fIEVersion == 10) {
return "IE/10";
}
} //isIE end
if (isIE11) {
return "IE/11";
}
//firefox
if (agent.indexOf("firefox") > 0) {
return agent.match(regStr_ff);
}
//Safari
if (agent.indexOf("safari") > 0 && agent.indexOf("chrome") < 0) {
return agent.match(regStr_saf);
}
//Chrome
if (agent.indexOf("chrome") > 0) {
return agent.match(regStr_chrome);
}
//opera
if(agent.indexOf("opera")>0){
return agent.match(regStr_opera);
}
}