Javascript 判断浏览器版本和名称

var agent = navigator.userAgent.toLowerCase(),
browserVersion = null,
browserSet = ['msie','chrome','firefox','safari'];

browserSet.forEach(function(item){
//eg:In chrome ,navigator.userAgent is "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
var res = agent.match(new RegExp(item + '/[\\d+\\.]\+'));
if(res !== null){
browserVersion = res[0];
return false;
}
});
if(browserVersion === null){
//in IE11,navigator.userAgent is "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; InfoPath.3; rv:11.0) like Gecko"
    if('ActiveXObject' in window){
browserVersion = 'ie/11.0';
}
}

if(browserVersion === null){
browserVersion = 'is not a standard explorer.';
}
posted @ 2016-09-07 16:45  王仲春  阅读(135)  评论(0编辑  收藏  举报