var userAgent = navigator.userAgent,
rMsie = /(msie\s|trident.*rv:)([\w.]+)/,
rFirefox = /(firefox)\/([\w.]+)/,
rOpera = /(opera).+version\/([\w.]+)/,
rChrome = /(chrome)\/([\w.]+)/,
rSafari = /version\/([\w.]+).*(safari)/
var browser
var version
var ua = userAgent.toLowerCase()
function uaMatch(ua) {
var match = rMsie.exec(ua)
if (match != null) {
return { browser : "IE", version : match[2] || "0" }
}
var match = rFirefox.exec(ua)
if (match != null) {
return { browser : match[1] || "", version : match[2] || "0" }
}
var match = rOpera.exec(ua)
if (match != null) {
return { browser : match[1] || "", version : match[2] || "0" }
}
var match = rChrome.exec(ua)
if (match != null) {
return { browser : match[1] || "", version : match[2] || "0" }
}
var match = rSafari.exec(ua)
if (match != null) {
return { browser : match[2] || "", version : match[1] || "0" }
}
if (match != null) {
return { browser : "", version : "0" }
}
}
var browserMatch = uaMatch(userAgent.toLowerCase())
if (browserMatch.browser) {
browser = browserMatch.browser
version = browserMatch.version
}
document.write(browser+version)
</script>
isIE11 = (sUserAgent.toLowerCase().indexOf("trident") >-1 &&sUserAgent.indexOf("rv") >-1)IE6~IE10的浏览器特征明显,存在msie字符串,而IE11去掉了msie字符串,可以通过rv后面的版本号识别IE11,具体方法如下:
function IETester(userAgent){var UA = userAgent || navigator.userAgent
if(/msie/i.test(UA)){
return UA.match(/msie (\d+\.\d+)/i)[1]
}else if(~UA.toLowerCase().indexOf('trident') && ~UA.indexOf('rv')){
return UA.match(/rv:(\d+\.\d+)/)[1]
}
return false
}
IETester()//不传参数返回当前IE版本,如果不是IE内核浏览器,返回false
IETester('Mozilla/5.0 (Windows NT 6.3 WOW64 Trident/7.0 rv:11.0) like Gecko')//传userAgent字符串,用来判断其他IE浏览器的版本,该示例返回11.0