Detect IE version (prior to v9) in JavaScript

前端 未结 30 1440
半阙折子戏
半阙折子戏 2020-11-22 08:44

I want to bounce users of our web site to an error page if they\'re using a version of Internet Explorer prior to v9. It\'s just not worth our time and money to

相关标签:
30条回答
  • 2020-11-22 08:51

    or simply

    //   IE 10: ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)'; 
    //   IE 11: ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko'; 
    // Edge 12: ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0'; 
    // Edge 13: ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586'; 
    
    var isIE = navigator.userAgent.match(/MSIE|Trident|Edge/)
    var IEVersion = ((navigator.userAgent.match(/(?:MSIE |Trident.*rv:|Edge\/)(\d+(\.\d+)?)/)) || []) [1]
    
    0 讨论(0)
  • 2020-11-22 08:53

    To detect Internet Explorer 10|11 you can use this little script immediatelly after body tag:

    In my case i use jQuery library loaded in head.

    <!DOCTYPE HTML>
    <html>
    <head>
        <script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
    </head>
    <body>
        <script>if (navigator.appVersion.indexOf('Trident/') != -1) $("body").addClass("ie10");</script>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-22 08:53

    The most comprehensive JS script I found to check for versions of IE is http://www.pinlady.net/PluginDetect/IE/. The entire library is at http://www.pinlady.net/PluginDetect/Browsers/.

    With IE10, conditional statements are no longer supported.

    With IE11, the user agent no longer contains MSIE. Also, using the user agent is not reliable because that can be modified.

    Using the PluginDetect JS script, you can detect for IE and detect the exact versions by using very specific and well-crafted code that targets specific IE versions. This is very useful when you care exactly what version of browser you are working with.

    0 讨论(0)
  • 2020-11-22 08:55

    This has been answered to death, but this is all you need.

    !!navigator.userAgent.match(/msie\s[5-8]/i)
    
    0 讨论(0)
  • 2020-11-22 08:57

    According to Microsoft, following is the best solution, it is also very simple:

    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 >= 8.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)
  • 2020-11-22 08:58

    To reliably filter IE8 and older, checking global objects can be used:

    if (document.all && !document.addEventListener) {
        alert('IE8 or lower');
    }
    
    0 讨论(0)
提交回复
热议问题