How to detect IE 11 with javascript in Asp.net

后端 未结 10 1893
暖寄归人
暖寄归人 2020-12-01 05:04

Hello I want to detect the Browser , IE 8 or more will be appropriate for me. For this i used following code but it fails for IE 11 . For other its detecting properly.

相关标签:
10条回答
  • 2020-12-01 05:20

    As already stated - dont do browser detection, rather do feature detection. However as I see your script is an older version of a script the circulates around here. This is the updated version that detects IE 11 aswel:

    function getInternetExplorerVersion()
                                {
                                    var rv = -1;
                                    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 );
                                    }
                                    else if (navigator.appName == 'Netscape')
                                    {
                                        var ua = navigator.userAgent;
                                        var re  = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
                                        if (re.exec(ua) != null)
                                            rv = parseFloat( RegExp.$1 );
                                    }
                                    return rv;
                                }
    
    0 讨论(0)
  • 2020-12-01 05:22

    Is better for you if you avoid browser detection; if you need it here is a good explain from MS team:

    In rare cases, it may be necessary to uniquely identify IE11 Preview. Use the Trident token to do so

    User-agent string changes

    For many legacy websites, some of the most visible updates for IE11 Preview involve the user-agent string. Here's what's reported for IE11 Preview on Windows 8.1 Preview: JavaScript

    Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko

    As with previous versions of Internet Explorer, portions of user-agent string vary according to the environment. Here's the string for IE11 Preview on Windows 7: JavaScript

    Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko

    If you compare these strings to the ones reported by earlier versions of Internet Explorer, you'll find the following changes: The compatible ("compatible") and browser ("MSIE") tokens have been removed. The "like Gecko" token has been added (for consistency with other browsers). The version of the browser is now reported by a new revision ("rv") token. These changes help prevent IE11 Preview from being (incorrectly) identified as an earlier version. In general, you should avoid detecting specific browsers or browser versions. The assumptions underlying such tests tend to lead to false positive results when browsers are updated. Instead, detect features as you need them and use progressive enhancement to provide simplified experiences for browsers or devices that do not support the features you need. In rare cases, it may be necessary to uniquely identify IE11 Preview. Use the Trident token to do so

    Link: http://msdn.microsoft.com/en-us/library/ie/bg182625(v=vs.85).aspx

    0 讨论(0)
  • 2020-12-01 05:24

    feature detect, feature detect, feature detect

            <script>
    
        if (!('querySelector' in document)  //this should work in ie 9+
             || !('localStorage' in window)  //ie 8+
             || !('addEventListener' in window)  //ie 8 + (I think)
            || !('matchMedia' in window)) {//ie 10+
    
            //do your redirect here
        }
    
    </script>
    
    0 讨论(0)
  • 2020-12-01 05:28

    More easy and efficient code and detect all versions of IE/Edge:

    if(navigator.appVersion.indexOf("MSIE") != -1 || navigator.appVersion.indexOf("Trident") != -1 || navigator.appVersion.indexOf("Edge") != -1){
    // is IE
    }
    
    0 讨论(0)
提交回复
热议问题