IE 11的userAgent版本为Trident x.x
判断IE 11 用正则判断userAgent中有没有带Trident字样的字符串即可
var u = window.navigator.userAgent.toLocaleLowerCase(),
ie11 = /(trident)\/([\d.]+)/,
b = u.match(ie11)
if(b){
alert("该浏览器是ie11")
}
<script type="text/javascript">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>