Differentiate IE7 browser and browser in IE7 compatibility mode

后端 未结 5 1500
半阙折子戏
半阙折子戏 2020-11-27 07:00

Can I differentiate if client\'s browser is IE7 or e.g. IE9 in IE7 compatibility mode? I\'m trying to figure out if I can do a JS check on my site which would recognize two

相关标签:
5条回答
  • 2020-11-27 07:07

    I don't believe there is a way to detect if the user's browser is in compat mode. Their user agent string will be determined by their browser mode, and their document mode will be determined by either the presence of an x-ua-compatible meta tag (or header), or possibly by the doctype used.

    Compatibility Mode was meant to protect the modern-browser-user from pages that relied on old and outdated features or hacks. It's not really something you would want to test against. Instead, write standards-compliant code which will be understood by the browser in either compat mode, or non-compat mode.

    Here are the various results of differing Browser Modes and Document Modes:

    Browser Mode: IE10 Compat View / Document Mode: IE7 standards

    navigator.userAgent
     
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/6.0; 
    .NET4.0E; .NET4.0C; Media Center PC 6.0; .NET CLR 3.5.30729; 
    .NET CLR 2.0.50727; .NET CLR 3.0.30729; BRI/2)" 
    
    document.documentMode
    
    7
    

    Browser Mode: IE7 / Document Mode: IE7 standards

    navigator.userAgent
    
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; .NET4.0E; 
    .NET4.0C; Media Center PC 6.0; .NET CLR 3.5.30729; .NET CLR 2.0.50727; 
    .NET CLR 3.0.30729; BRI/2)"
    
    document.documentMode
    
    7
    

    As you can see, by these two methods there is no way to tell if the user is in compat view or not.

    Overriding Compat View List

    If your site appears on the compatibility view list, you can override their suggested rendering options by providng your own x-ua-compatible header:

    <meta http-equiv="x-ua-compatible" content="IE=9" />
    

    This forces your browser into IE9 Standards Mode (no evaluation of the doctype). You could use IE=edge to force it into the latest mode possible (on Internet Explorer 10, this would be IE 10 Standards), but this is not encouraged. Instead, set it to the latest mode you've tested with.

    If the x-ua-compatible header is set to IE10, but the user visits your page on an earlier browser, the nearest rendering engine will be used. For instance, if the user visits your page with IE9, and your meta tag instructs the browser to use IE10, the browser will fallback to IE9 Standards mode.

    Note that IE=9 causes the browser to go into IE9 Standards Mode. It doesn't necessarily cause the browser to behave as though it were IE9. If you want the browser to behave as though it were IE9, you would want to use the EmulateIE9 content:

    <meta http-equiv="x-ua-compatible" content="IE=EmulateIE9" />
    

    This causes the browser to fallback on the DOCTYPE, if it's present, to determine whether the document mode will be Standards, or Quirks.

    For further information, see Defining Document Compatibility.

    0 讨论(0)
  • 2020-11-27 07:08

    Here is a fairly bullet-proof check that I did for a large-scale auction site based in San Jose.

    var userAgentString = navigator.userAgent;
    
    if(/MSIE 7\.0/gi.test(userAgentString) && /Trident/gi.test(userAgentString)){
        // compatibility mode
        // .... code goes here ....
    }
    
    0 讨论(0)
  • 2020-11-27 07:21

    This works for me:

    /**
     * Actual IE8-Browser running in IE8-Compat-Mode or IE7 Mode?
     */
    MyUtils.isIE8CompatMode= function(){
        if($.browser.msie && navigator.userAgent.indexOf('MSIE 7.0')>=0){
            return true;
        }
        return false;
    }
    

    An actual IE8 browser has 3 Document-Modes (IE7, IE8 & Quirks) and 3-Browser-Modes (IE7, IE8 and IE8-Compatibility). We can enforce the Document-Mode to be IE8 with:

     <meta http-equiv="X-UA-Compatible" content="IE=8,chrome=1"/>
    

    But, we cannot enforce the Browser-Mode, which is determined by Browser-configuration. For example: some of our customers have the "render all pages in compatibility-mode"-checkbox checked in their Compatibility-View-Settings-Dialog. If that is the case, we can do nothing about it.

    See the very enlightening diagram on this site:

    -> How ie8 determines compatibility-mode

    What do we use the above function for? Since our Page shows some glitches when in compatibility-mode we need to tell the user, why it is looking bad and what he can do about it. So we use the above function to tell if we are in trouble and then render a small warning to the user.

    In ie9 and ie10 compatiblity-modes our app looks fine, so we dont need it there. This response may be considered an answer to this question: detect ie8 compatibility mode which was marked as a duplicate of this one. Perhaps it helps someone.

    0 讨论(0)
  • 2020-11-27 07:28

    For anyone reaching this thread on Google, here is another option:

    Using the script located at http://www.quirksmode.org/js/detect.html....

    if (BrowserDetect.browser == 'Explorer')
    {
        if (document.documentMode == 7 && BrowserDetect.version > 7)
        {
            // user is running IE in compatability mode
        }
    }
    
    0 讨论(0)
  • 2020-11-27 07:31

    For at least IE8 and IE9, you can check whether navigator.userAgent has the substring Trident in it. An IE8+ always has a Trident in its user-agent, where an IE7 doesn't. See this answer and the MSDN link in it.

    IE10 seems trickier: it is reported in the comments below that Trident is not always present with IE7 emulation mode. Probably the OS string (eg. Windows NT 6.2) will still reveal IE10, if IE10 will not be available on any platform where IE7 is available.

    Please also note that the HTTP User-Agent header might not always match navigator.userAgent. This is the case at least with IE9 that has compatiblity mode on (sends an IE7 User-Agent header) but detects something like IE=Edge in the response (navigator.userAgent turns back to IE9).

    0 讨论(0)
提交回复
热议问题