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条回答
The star\"
2楼-- · 2019-03-12 10:43

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.

<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

Alternatively you could just roll your own solution using code plagarised from jQuery.

var userAgent = navigator.userAgent.toLowerCase();
var version = (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
var isIE = /msie/.test( userAgent ) && !/opera/.test( userAgent ),    
查看更多
Fickle 薄情
3楼-- · 2019-03-12 10:43

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'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;
查看更多
放荡不羁爱自由
4楼-- · 2019-03-12 10:46

The Navigator object contains all the information about the user's browser:

eg:

var browser=navigator.appName;

var b_version=navigator.appVersion;

var version=parseFloat(b_version);

See:

http://www.w3schools.com/js/js_browser.asp

查看更多
女痞
5楼-- · 2019-03-12 10:47

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...

查看更多
Fickle 薄情
6楼-- · 2019-03-12 10:47

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.

You can easily put a conditional comment — a JScript conditional comment, not an HTML one — inside a function:

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.

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.

查看更多
等我变得足够好
7楼-- · 2019-03-12 10:55

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.

查看更多
登录 后发表回答