In some existing code there is a test to see if the user is running IE, by checking if the object Browser.Engine.trident is defined and returns true.
在一些现有代码中,通过检查对象Browser.Engine.trident是否已定义并返回true,进行测试以查看用户是否正在运行IE。
But how can I determine if the user is running IE6 (or earlier) or IE7 (or later)?
但是如何确定用户是在运行IE6(或更早版本)还是运行IE7(或更高版本)?
The test is needed inside a JavaScript function so a conditional comment doesn't seem suitable.
JavaScript函数内部需要进行测试,因此条件注释似乎不合适。
12 个解决方案
#1
18
From detecting Internet Explorer More Effectively at msdn:
从msdn更有效地检测Internet Explorer:
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
function checkVersion()
{
var msg = "You're not using Internet Explorer.";
var ver = getInternetExplorerVersion();
if ( ver > -1 )
{
if ( ver >= 6.0 )
msg = "You're using a recent copy of Internet Explorer."
else
msg = "You should upgrade your copy of Internet Explorer.";
}
alert( msg );
}
#2
12
If you really want to be sure you are using IE and a specific version then you could obviously use IE's conditional tags to only run certain code within IE. It's not really that pretty but at least you can be sure that it is really IE and not some spoofed version.
如果你真的想确定你使用IE和特定版本,那么显然你可以使用IE的条件标签只在IE中运行某些代码。它不是那么漂亮,但至少你可以确定它真的是IE而不是一些欺骗版本。
<script>
var isIE = false;
var version = -1;
</script>
<!--[if IE 6]>
<script>
isIE = true;
version = 6
</script>
<![endif]-->
<!--[if IE 7]>
<script>
isIE = true;
version = 7
</script>
<![endif]-->
It's pretty self explanatory. In IE6 isIE
is true
and version
is 6
, In IE7 isIE
is true
and version
is 7
otherwise isIE
is false and version
is -1
这是非常自我解释的。在IE6中isIE为true且版本为6,在IE7中isIE为true且版本为7,否则isIE为false且版本为-1
Alternatively you could just roll your own solution using code plagarised from jQuery.
或者,你可以使用jQuery中的代码来推广自己的解决方案。
var userAgent = navigator.userAgent.toLowerCase();
var version = (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
var isIE = /msie/.test( userAgent ) && !/opera/.test( userAgent ),
#3
4
If you are already using jQuery in a pre-1.9 version AND you don't need to detect IE 11, you can do this:
如果您已经在1.9之前的版本中使用jQuery并且您不需要检测IE 11,则可以执行以下操作:
if (jQuery.browser.msie == true) {
if (jQuery.browser.version == 7.0)
// .. do something for 7.0
else
// .. do something for < 7.0
}
#4
3
The Navigator object contains all the information about the user's browser:
Navigator对象包含有关用户浏览器的所有信息:
eg:
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
See:
#5
3
If you are checking for a certain functionality, you should check for it directly, e.g. if (window.focus) {window.focus();}
Browser detection is never reliable enough.
如果您要检查某项功能,则应直接检查,例如if(window.focus){window.focus();}浏览器检测永远不够可靠。
For more details on object vs browser detection, check out this article at Quirksmode.
有关对象与浏览器检测的更多详细信息,请参阅Quirksmode上的这篇文章。
On the other hand, if the feature you need IS the browser type and version, e.g. for statistical purposes, go with navigator.appName
and navigator.appVersion
. (Beware though - many less popular browsers masquerade themselves as MSIE 6 or 7, as certain sites block anything that's not IE on the premise that "all the modern browsers are IE, right?" (hint: not anymore).)
另一方面,如果您需要的功能是浏览器类型和版本,例如出于统计目的,请使用navigator.appName和navigator.appVersion。 (请注意 - 许多不那么受欢迎的浏览器将自己伪装成MSIE 6或7,因为某些网站在“所有现代浏览器都是IE,对吧?”的前提下阻止任何不是IE的东西。(提示:不再)。)
#6
3
So IE8 compatibility view mode reports itself as IE7 even though it doesn't always behave the same. And for that, I give you this monster:
所以IE8兼容性视图模式将自身报告为IE7,即使它并不总是表现相同。为此,我给你这个怪物:
// IE8's "Compatibility mode" is anything but. Oh well, at least it doesn't take 40 lines of code to detect and work around it.
// Oh wait:
/*
* Author: Rob Reid
* CreateDate: 20-Mar-09
* Description: Little helper function to return details about IE 8 and its various compatibility settings either use as it is
* or incorporate into a browser object. Remember browser sniffing is not the best way to detect user-settings as spoofing is
* very common so use with caution.
*/
function IEVersion(){
var _n=navigator,_w=window,_d=document;
var version="NA";
var na=_n.userAgent;
var ieDocMode="NA";
var ie8BrowserMode="NA";
// Look for msie and make sure its not opera in disguise
if(/msie/i.test(na) && (!_w.opera)){
// also check for spoofers by checking known IE objects
if(_w.attachEvent && _w.ActiveXObject){
// Get version displayed in UA although if its IE 8 running in 7 or compat mode it will appear as 7
version = (na.match( /.+ie\s([\d.]+)/i ) || [])[1];
// Its IE 8 pretending to be IE 7 or in compat mode
if(parseInt(version)==7){
// documentMode is only supported in IE 8 so we know if its here its really IE 8
if(_d.documentMode){
version = 8; //reset? change if you need to
// IE in Compat mode will mention Trident in the useragent
if(/trident\/\d/i.test(na)){
ie8BrowserMode = "Compat Mode";
// if it doesn't then its running in IE 7 mode
}else{
ie8BrowserMode = "IE 7 Mode";
}
}
}else if(parseInt(version)==8){
// IE 8 will always have documentMode available
if(_d.documentMode){ ie8BrowserMode = "IE 8 Mode";}
}
// If we are in IE 8 (any mode) or previous versions of IE we check for the documentMode or compatMode for pre 8 versions
ieDocMode = (_d.documentMode) ? _d.documentMode : (_d.compatMode && _d.compatMode=="CSS1Compat") ? 7 : 5;//default to quirks mode IE5
}
}
return {
"UserAgent" : na,
"Version" : version,
"BrowserMode" : ie8BrowserMode,
"DocMode": ieDocMode
}
}
var ieVersion = IEVersion();
var IsIE8 = ieVersion.Version != "NA" && ieVersion.Version >= 8;
#7
2
This is probably going to get voted down, because it's not directly answering the question, but... You should not be writing browser-specific code. There's very little you can't do while coding for most widely-accepted browsers.
这可能会被拒绝,因为它没有直接回答这个问题,但是......你不应该编写特定于浏览器的代码。在为最广泛接受的浏览器编码时,你几乎无法做到。
EDIT: The only time I found it useful to have conditional comments was when I needed to include ie6.css or ie7.css.
编辑:我发现有条件评论的唯一时间是我需要包括ie6.css或ie7.css。
#8
1
Well... here is what I came up after thinking for a while. just wanted to find a simple solution.
嗯......这是我想了一会儿后才出现的。只是想找一个简单的解决方案。
if (navigator.appName == 'Microsoft Internet Explorer') {
// Take navigator appversion to an array & split it
var appVersion = navigator.appVersion.split(';');
// Get the part that you want from the above array
var verNumber = appVersion[1];
alert(verNumber);
}
It returns ex:- MSIE 10.0, MSIE 9.0, MSIE 8.0
它返回ex: - MSIE 10.0,MSIE 9.0,MSIE 8.0
further extend, if you want to check if it's "lower than" or "greater than" IE version, you can slightly modify
进一步扩展,如果你想检查它是“低于”还是“大于”IE版本,你可以稍加修改
if (navigator.appName == 'Microsoft Internet Explorer') {
var appVersion = navigator.appVersion.split(';');
var verNumber = appVersion[1];
// Reaplce "MSIE " from the srting and parse it to integer value
var IEversion = parseInt(verNumber.replace('MSIE ', ''));
if(IEversion <= 9){
alert(verNumber);
}
}
got the base idea from w3schools, hope this will help some one... :D
从w3schools得到了基本的想法,希望这会帮助一些人......:D
#9
0
This is the script I use and it seems to work well enough:
这是我使用的脚本,似乎运行得很好:
// Returns 0 if the browser is anything but IE
function getIEVersion() {
var ua = window.navigator.userAgent;
var ie = ua.indexOf("MSIE ");
return ((ie > 0) ? parseInt(ua.substring(ie+5, ua.indexOf(".", ie))) : 0);
}
Hope that helps someone...
希望有人帮助......
#10
0
This should give you more details than you'll want:
这应该会为您提供比您想要的更多详细信息:
var agent = navigator.userAgent;
var msiePattern = /.*MSIE ((\d+).\d+).*/
if( msiePattern.test( agent ) ) {
var majorVersion = agent.replace(msiePattern,"$2");
var fullVersion = agent.replace(msiePattern,"$1");
var majorVersionInt = parseInt( majorVersion );
var fullVersionFloat = parseFloat( fullVersion );
}
#11
0
As no-one seems to have said it yet:
似乎没有人说过它:
The test is needed inside a JavaScript function so a conditional comment doesn't seem suitable.
JavaScript函数内部需要进行测试,因此条件注释似乎不合适。
You can easily put a conditional comment — a JScript conditional comment, not an HTML one — inside a function:
您可以轻松地在函数内部添加条件注释 - JScript条件注释,而不是HTML注释:
function something() {
var IE_WIN= false;
var IE_WIN_7PLUS= false;
/*@cc_on
@if (@_win32)
IE_WIN= true;
@if (@_jscript_version>=5.7)
IE_WIN_7PLUS = true;
@end
@end @*/
...
}
It's more typical to do the test once at global level though, and just check the stored flags thereafter.
在全局级别进行一次测试更为典型,然后只检查存储的标志。
CCs are more reliable than sifting through the mess that the User-Agent string has become these days. String matching methods on navigator.userAgent can misidentify spoofing browsers such as Opera.
CC比用户代理字符串最近变得混乱更可靠。 navigator.userAgent上的字符串匹配方法可能会错误地识别诸如Opera之类的欺骗性浏览器。
Of course capability sniffing is much better for cross-browser code where it's possible, but for some cases — usually bug fix workarounds — you do need to identify IE specifically, and CCs are probably the best way to do that today.
当然,对于可能的跨浏览器代码,功能嗅探要好得多,但在某些情况下 - 通常是错误修复解决方法 - 你需要专门识别IE,而CC可能是今天最好的方法。
#12
0
<script>
alert("It is " + isIE());
//return ie number as int else return false
function isIE() {
var myNav = navigator.userAgent.toLowerCase();
if (myNav.indexOf('msie') != -1) //ie less than ie11 (6-10)
{
return parseInt(myNav.split('msie')[1]);
}
else
{
//Is the version more than ie11? Then return false else return ie int number
return (!!(myNav.match(/trident/) && !myNav.match(/msie/)) == false)?false : parseInt(myNav.split('rv:')[1].substring(0, 2));
}
}
</script>
#1
18
From detecting Internet Explorer More Effectively at msdn:
从msdn更有效地检测Internet Explorer:
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
function checkVersion()
{
var msg = "You're not using Internet Explorer.";
var ver = getInternetExplorerVersion();
if ( ver > -1 )
{
if ( ver >= 6.0 )
msg = "You're using a recent copy of Internet Explorer."
else
msg = "You should upgrade your copy of Internet Explorer.";
}
alert( msg );
}
#2
12
If you really want to be sure you are using IE and a specific version then you could obviously use IE's conditional tags to only run certain code within IE. It's not really that pretty but at least you can be sure that it is really IE and not some spoofed version.
如果你真的想确定你使用IE和特定版本,那么显然你可以使用IE的条件标签只在IE中运行某些代码。它不是那么漂亮,但至少你可以确定它真的是IE而不是一些欺骗版本。
<script>
var isIE = false;
var version = -1;
</script>
<!--[if IE 6]>
<script>
isIE = true;
version = 6
</script>
<![endif]-->
<!--[if IE 7]>
<script>
isIE = true;
version = 7
</script>
<![endif]-->
It's pretty self explanatory. In IE6 isIE
is true
and version
is 6
, In IE7 isIE
is true
and version
is 7
otherwise isIE
is false and version
is -1
这是非常自我解释的。在IE6中isIE为true且版本为6,在IE7中isIE为true且版本为7,否则isIE为false且版本为-1
Alternatively you could just roll your own solution using code plagarised from jQuery.
或者,你可以使用jQuery中的代码来推广自己的解决方案。
var userAgent = navigator.userAgent.toLowerCase();
var version = (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
var isIE = /msie/.test( userAgent ) && !/opera/.test( userAgent ),
#3
4
If you are already using jQuery in a pre-1.9 version AND you don't need to detect IE 11, you can do this:
如果您已经在1.9之前的版本中使用jQuery并且您不需要检测IE 11,则可以执行以下操作:
if (jQuery.browser.msie == true) {
if (jQuery.browser.version == 7.0)
// .. do something for 7.0
else
// .. do something for < 7.0
}
#4
3
The Navigator object contains all the information about the user's browser:
Navigator对象包含有关用户浏览器的所有信息:
eg:
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
See:
#5
3
If you are checking for a certain functionality, you should check for it directly, e.g. if (window.focus) {window.focus();}
Browser detection is never reliable enough.
如果您要检查某项功能,则应直接检查,例如if(window.focus){window.focus();}浏览器检测永远不够可靠。
For more details on object vs browser detection, check out this article at Quirksmode.
有关对象与浏览器检测的更多详细信息,请参阅Quirksmode上的这篇文章。
On the other hand, if the feature you need IS the browser type and version, e.g. for statistical purposes, go with navigator.appName
and navigator.appVersion
. (Beware though - many less popular browsers masquerade themselves as MSIE 6 or 7, as certain sites block anything that's not IE on the premise that "all the modern browsers are IE, right?" (hint: not anymore).)
另一方面,如果您需要的功能是浏览器类型和版本,例如出于统计目的,请使用navigator.appName和navigator.appVersion。 (请注意 - 许多不那么受欢迎的浏览器将自己伪装成MSIE 6或7,因为某些网站在“所有现代浏览器都是IE,对吧?”的前提下阻止任何不是IE的东西。(提示:不再)。)
#6
3
So IE8 compatibility view mode reports itself as IE7 even though it doesn't always behave the same. And for that, I give you this monster:
所以IE8兼容性视图模式将自身报告为IE7,即使它并不总是表现相同。为此,我给你这个怪物:
// IE8's "Compatibility mode" is anything but. Oh well, at least it doesn't take 40 lines of code to detect and work around it.
// Oh wait:
/*
* Author: Rob Reid
* CreateDate: 20-Mar-09
* Description: Little helper function to return details about IE 8 and its various compatibility settings either use as it is
* or incorporate into a browser object. Remember browser sniffing is not the best way to detect user-settings as spoofing is
* very common so use with caution.
*/
function IEVersion(){
var _n=navigator,_w=window,_d=document;
var version="NA";
var na=_n.userAgent;
var ieDocMode="NA";
var ie8BrowserMode="NA";
// Look for msie and make sure its not opera in disguise
if(/msie/i.test(na) && (!_w.opera)){
// also check for spoofers by checking known IE objects
if(_w.attachEvent && _w.ActiveXObject){
// Get version displayed in UA although if its IE 8 running in 7 or compat mode it will appear as 7
version = (na.match( /.+ie\s([\d.]+)/i ) || [])[1];
// Its IE 8 pretending to be IE 7 or in compat mode
if(parseInt(version)==7){
// documentMode is only supported in IE 8 so we know if its here its really IE 8
if(_d.documentMode){
version = 8; //reset? change if you need to
// IE in Compat mode will mention Trident in the useragent
if(/trident\/\d/i.test(na)){
ie8BrowserMode = "Compat Mode";
// if it doesn't then its running in IE 7 mode
}else{
ie8BrowserMode = "IE 7 Mode";
}
}
}else if(parseInt(version)==8){
// IE 8 will always have documentMode available
if(_d.documentMode){ ie8BrowserMode = "IE 8 Mode";}
}
// If we are in IE 8 (any mode) or previous versions of IE we check for the documentMode or compatMode for pre 8 versions
ieDocMode = (_d.documentMode) ? _d.documentMode : (_d.compatMode && _d.compatMode=="CSS1Compat") ? 7 : 5;//default to quirks mode IE5
}
}
return {
"UserAgent" : na,
"Version" : version,
"BrowserMode" : ie8BrowserMode,
"DocMode": ieDocMode
}
}
var ieVersion = IEVersion();
var IsIE8 = ieVersion.Version != "NA" && ieVersion.Version >= 8;
#7
2
This is probably going to get voted down, because it's not directly answering the question, but... You should not be writing browser-specific code. There's very little you can't do while coding for most widely-accepted browsers.
这可能会被拒绝,因为它没有直接回答这个问题,但是......你不应该编写特定于浏览器的代码。在为最广泛接受的浏览器编码时,你几乎无法做到。
EDIT: The only time I found it useful to have conditional comments was when I needed to include ie6.css or ie7.css.
编辑:我发现有条件评论的唯一时间是我需要包括ie6.css或ie7.css。
#8
1
Well... here is what I came up after thinking for a while. just wanted to find a simple solution.
嗯......这是我想了一会儿后才出现的。只是想找一个简单的解决方案。
if (navigator.appName == 'Microsoft Internet Explorer') {
// Take navigator appversion to an array & split it
var appVersion = navigator.appVersion.split(';');
// Get the part that you want from the above array
var verNumber = appVersion[1];
alert(verNumber);
}
It returns ex:- MSIE 10.0, MSIE 9.0, MSIE 8.0
它返回ex: - MSIE 10.0,MSIE 9.0,MSIE 8.0
further extend, if you want to check if it's "lower than" or "greater than" IE version, you can slightly modify
进一步扩展,如果你想检查它是“低于”还是“大于”IE版本,你可以稍加修改
if (navigator.appName == 'Microsoft Internet Explorer') {
var appVersion = navigator.appVersion.split(';');
var verNumber = appVersion[1];
// Reaplce "MSIE " from the srting and parse it to integer value
var IEversion = parseInt(verNumber.replace('MSIE ', ''));
if(IEversion <= 9){
alert(verNumber);
}
}
got the base idea from w3schools, hope this will help some one... :D
从w3schools得到了基本的想法,希望这会帮助一些人......:D
#9
0
This is the script I use and it seems to work well enough:
这是我使用的脚本,似乎运行得很好:
// Returns 0 if the browser is anything but IE
function getIEVersion() {
var ua = window.navigator.userAgent;
var ie = ua.indexOf("MSIE ");
return ((ie > 0) ? parseInt(ua.substring(ie+5, ua.indexOf(".", ie))) : 0);
}
Hope that helps someone...
希望有人帮助......
#10
0
This should give you more details than you'll want:
这应该会为您提供比您想要的更多详细信息:
var agent = navigator.userAgent;
var msiePattern = /.*MSIE ((\d+).\d+).*/
if( msiePattern.test( agent ) ) {
var majorVersion = agent.replace(msiePattern,"$2");
var fullVersion = agent.replace(msiePattern,"$1");
var majorVersionInt = parseInt( majorVersion );
var fullVersionFloat = parseFloat( fullVersion );
}
#11
0
As no-one seems to have said it yet:
似乎没有人说过它:
The test is needed inside a JavaScript function so a conditional comment doesn't seem suitable.
JavaScript函数内部需要进行测试,因此条件注释似乎不合适。
You can easily put a conditional comment — a JScript conditional comment, not an HTML one — inside a function:
您可以轻松地在函数内部添加条件注释 - JScript条件注释,而不是HTML注释:
function something() {
var IE_WIN= false;
var IE_WIN_7PLUS= false;
/*@cc_on
@if (@_win32)
IE_WIN= true;
@if (@_jscript_version>=5.7)
IE_WIN_7PLUS = true;
@end
@end @*/
...
}
It's more typical to do the test once at global level though, and just check the stored flags thereafter.
在全局级别进行一次测试更为典型,然后只检查存储的标志。
CCs are more reliable than sifting through the mess that the User-Agent string has become these days. String matching methods on navigator.userAgent can misidentify spoofing browsers such as Opera.
CC比用户代理字符串最近变得混乱更可靠。 navigator.userAgent上的字符串匹配方法可能会错误地识别诸如Opera之类的欺骗性浏览器。
Of course capability sniffing is much better for cross-browser code where it's possible, but for some cases — usually bug fix workarounds — you do need to identify IE specifically, and CCs are probably the best way to do that today.
当然,对于可能的跨浏览器代码,功能嗅探要好得多,但在某些情况下 - 通常是错误修复解决方法 - 你需要专门识别IE,而CC可能是今天最好的方法。
#12
0
<script>
alert("It is " + isIE());
//return ie number as int else return false
function isIE() {
var myNav = navigator.userAgent.toLowerCase();
if (myNav.indexOf('msie') != -1) //ie less than ie11 (6-10)
{
return parseInt(myNav.split('msie')[1]);
}
else
{
//Is the version more than ie11? Then return false else return ie int number
return (!!(myNav.match(/trident/) && !myNav.match(/msie/)) == false)?false : parseInt(myNav.split('rv:')[1].substring(0, 2));
}
}
</script>