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
/判断当前浏览类型
function BrowserType()
{
var userAgent = navigator.userAgent //取得浏览器的userAgent字符串
var isOpera = userAgent.indexOf("Opera") > -1 //判断是否Opera浏览器
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera //判断是否IE浏览器
var isEdge = userAgent.indexOf("Windows NT 6.1 Trident/7.0") > -1 && !isIE //判断是否IE的Edge浏览器
var isFF = userAgent.indexOf("Firefox") > -1 //判断是否Firefox浏览器
var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1 //判断是否Safari浏览器
var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1 //判断Chrome浏览器
if (isIE)
{
var reIE = new RegExp("MSIE (\\d+\\.\\d+)")
reIE.test(userAgent)
var fIEVersion = parseFloat(RegExp["$1"])
if(fIEVersion == 7)
{ return "IE7"}
else if(fIEVersion == 8)
{ return "IE8"}
else if(fIEVersion == 9)
{ return "IE9"}
else if(fIEVersion == 10)
{ return "IE10"}
else if(fIEVersion == 11)
{ return "IE11"}
else
{ return "0"}//IE版本过低
}//isIE end
if (isFF) { return "FF"}
if (isOpera) { return "Opera"}
if (isSafari) { return "Safari"}
if (isChrome) { return "Chrome"}
if (isEdge) { return "Edge"}
}//myBrowser() end
//判断是否是IE浏览器
function isIE()
{
var userAgent = navigator.userAgent //取得浏览器的userAgent字符串
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera //判断是否IE浏览器
if(isIE)
{
return "1"
}
else
{
return "-1"
}
}
//判断是否是IE浏览器,包括Edge浏览器
function IEVersion()
{
var userAgent = navigator.userAgent //取得浏览器的userAgent字符串
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera //判断是否IE浏览器
var isEdge = userAgent.indexOf("Windows NT 6.1 Trident/7.0") > -1 && !isIE //判断是否IE的Edge浏览器
if(isIE)
{
var reIE = new RegExp("MSIE (\\d+\\.\\d+)")
reIE.test(userAgent)
var fIEVersion = parseFloat(RegExp["$1"])
if(fIEVersion == 7)
{ return "IE7"}
else if(fIEVersion == 8)
{ return "IE8"}
else if(fIEVersion == 9)
{ return "IE9"}
else if(fIEVersion == 10)
{ return "IE10"}
else if(fIEVersion == 11)
{ return "IE11"}
else
{ return "0"}//IE版本过低
}
else if(isEdge)
{
return "Edge"
}
else
{
return "-1"//非IE
}
}