How can I determine which version of IE a user is

2019-03-12 10:01发布

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.

But how can I determine if the user is running IE6 (or earlier) or IE7 (or later)?

The test is needed inside a JavaScript function so a conditional comment doesn't seem suitable.

12条回答
2楼-- · 2019-03-12 10:55
<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>
查看更多
家丑人穷心不美
3楼-- · 2019-03-12 10:59

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.

For more details on object vs browser detection, check out this article at 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).)

查看更多
Bombasti
4楼-- · 2019-03-12 10:59

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

further extend, if you want to check if it's "lower than" or "greater than" IE version, you can slightly modify

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

查看更多
啃猪蹄的小仙女
5楼-- · 2019-03-12 11:01

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:

if (jQuery.browser.msie == true) { 
if (jQuery.browser.version == 7.0)
  // .. do something for 7.0
else 
  // .. do something for < 7.0
}
查看更多
啃猪蹄的小仙女
6楼-- · 2019-03-12 11:03

From detecting Internet Explorer More Effectively at msdn:

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 );
}
查看更多
老娘就宠你
7楼-- · 2019-03-12 11:05

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 );
}
查看更多
登录 后发表回答