How can I determine which version of IE a user is running in JavaScript?

后端 未结 12 1860
生来不讨喜
生来不讨喜 2021-02-04 04:48

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 deter

相关标签:
12条回答
  • 2021-02-04 05:02
    <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>
    
    0 讨论(0)
  • 2021-02-04 05:04

    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;
    
    0 讨论(0)
  • 2021-02-04 05:05

    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

    0 讨论(0)
  • 2021-02-04 05:07

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

    0 讨论(0)
  • 2021-02-04 05:08

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

    0 讨论(0)
  • 2021-02-04 05:11

    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 );
    }
    
    0 讨论(0)
提交回复
热议问题