How to detect IE 11 with javascript in Asp.net

后端 未结 10 1892
暖寄归人
暖寄归人 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:08

    I used the following code recently to detect IE in general and it worked just fine for IE 11 as well

    var bs = document.body.style, isIE=false;
    if ('msTransition' in bs) {
            isIE = true;
    }
    

    The idea is to look for vedor prefix.

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

    The 'use feature detection' mantra mystifies me. When you hit a bug, how can you determine what 'feature' is the culprit? Take for example this bit of code:

            $('input[name^=qty]').keyup(function(e) {
                if (this.value.match(/[^0-9\/]*/g)) {
                    this.value = this.value.replace(/[^0-9\/]/g, '')
                }
            });
    

    It removes non-numeric chars from the input field as the user types. Works in FF, Chrome, and Safari. Does not work in ANY version of IE (up to 11, at least): any subsequent bindings on the input field will fail.

    0 讨论(0)
  • 2020-12-01 05:16
     isIE11 = !!window.MSStream;
    
     if(isIE11){
       /* Something */
     }
    
    0 讨论(0)
  • 2020-12-01 05:17

    DO NOT DO BROWSER DETECTION! It will break, and it will cause you problems.

    IE11 has a completely different User Agent string to previous IE versions; it not longer includes the "MSIE" text. This is why your detection code doesn't work.

    It's important to note here that the reason they did this was deliberate. They wanted to break browser detection scripts like this.

    It is possible to change your code to work with IE11, but I strongly recommend not doing this, as you'll probably have the same issue all over again when IE12 comes out.

    So why did they want to break browser detection scripts? Simple: Because IE11 does not have the bugs of previous versions, and it has a lot of new features. So if you're doing browser detection because IE has certain bugs or missing features and you've got code to fix those issues based on the browser detect, then that code might actually cause worse problems in IE11 where the fixes aren't needed.

    IE11 has broken your script, but the same logic applies to all browsers and all versions; detecting the browser and version is almost always the wrong thing to do.

    If there are specific features that you want to support, but are missing in older IE versions (or other older browsers), don't use browser detection to work it out; you should use feature detection instead.

    Feature detection means checking the browser to see whether it supports the particular features you want to use. The most common way of doing this is to use the Modernizr library. The docs on their site will guide you through setting it up.

    There are a few bugs in older IE versions which are hard to detect, and for these few cases, it is valid to use browser detection as a last resort, but these cases are really only for IE6 and earlier. Maybe occasionally for IE7. But you've stated in the question that you're only looking at IE8 and later, so that should not apply.

    Stick with feature detection; it's more reliable, better practice, and won't break suddenly when a new browser version is released.

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

    Use !(window.ActiveXObject) && "ActiveXObject" in window to detect IE11 explicitly.

    To detect any IE version, use window.ActiveXObject || "ActiveXObject" in window instead.

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

    You can explicitly detect IE11 with the following check (using feature detection):

    if (Object.hasOwnProperty.call(window, "ActiveXObject") && !window.ActiveXObject) {
        // is IE11
    }
    

    Explanation: All versions of IE (except really old ones) have window.ActiveXObject property present. IE11, however hides this property from DOM and that property is now undefined. But the property itself is present within object, so checking for property presence returns true in all IE versions, but in IE11 it also returns false for second check. And, finally, hasOwnProperty is called via Object because in IE8 (and I believe earlier) window is not an instanceof Object and does not have hasOwnProperty method.

    Another method, using userAgent string:

    var ua = window.navigator.userAgent;
    var versionSplit = /[\/\.]/i;
    var versionRe = /(Version)\/([\w.\/]+)/i; // match for browser version
    var operaRe = /(Opera|OPR)[\/ ]([\w.\/]+)/i;
    var ieRe = /(?:(MSIE) |(Trident)\/.+rv:)([\w.]+)/i; // must not contain 'Opera'
    var match = ua.match(operaRe) || ua.match(ieRe);
    if (!match) {
        return false;
    }
    if (Array.prototype.filter) {
        match = match.filter(function(item) {
            return (item != null);
        });
    } else {
        // Hello, IE8!
        for (var j = 0; j < match.length; j++) {
            var matchGroup = match[j];
            if (matchGroup == null || matchGroup == '') {
                match.splice(j, 1);
                j--;
            }
        }
    }
    var name = match[1].replace('Trident', 'MSIE').replace('OPR', 'Opera');
    var versionMatch = ua.match(versionRe) || match;
    var version = versionMatch[2].split(versionSplit);
    

    This will detect any version of IE if its userAgent string was not spoofed.

    There very rare cases when you actually need to use browser detection as described above. In most cases feature detection approach is preferable.

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