How can I detect Internet Explorer (IE) and Microsoft Edge using JavaScript?

前端 未结 13 867
小鲜肉
小鲜肉 2020-11-30 02:57

I\'ve looked around a lot, and I understand that there\'s a lot of ways to detect internet explorer.

My problem is this: I have an area on my HTML document, that when

相关标签:
13条回答
  • 2020-11-30 03:11

    For me better this:

    var uA = window.navigator.userAgent,
        onlyIEorEdge = /msie\s|trident\/|edge\//i.test(uA) && !!( document.uniqueID || window.MSInputMethodContext),
        checkVersion = (onlyIEorEdge && +(/(edge\/|rv:|msie\s)([\d.]+)/i.exec(uA)[2])) || NaN;
    

    Go run: http://output.jsbin.com/solicul/1/ o http://jsfiddle.net/Webnewbie/apa1nvu8/

    0 讨论(0)
  • 2020-11-30 03:11

    One line code to detect the browser.

    If the browser is IE or Edge, It will return true;

    let isIE = /edge|msie\s|trident\//i.test(window.navigator.userAgent)
    
    0 讨论(0)
  • 2020-11-30 03:12

    Here is a javascript class that detects IE10, IE11 and Edge.
    Navigator object is injected for testing purposes.

    var DeviceHelper = function (_navigator) {
        this.navigator = _navigator || navigator;
    };
    DeviceHelper.prototype.isIE = function() {
        if(!this.navigator.userAgent) {
            return false;
        }
    
        var IE10 = Boolean(this.navigator.userAgent.match(/(MSIE)/i)),
            IE11 = Boolean(this.navigator.userAgent.match(/(Trident)/i));
        return IE10 || IE11;
    };
    
    DeviceHelper.prototype.isEdge = function() {
        return !!this.navigator.userAgent && this.navigator.userAgent.indexOf("Edge") > -1;
    };
    
    DeviceHelper.prototype.isMicrosoftBrowser = function() {
        return this.isEdge() || this.isIE();
    };
    
    0 讨论(0)
  • 2020-11-30 03:15

    I don't know why, but I'm not seeing "Edge" in the userAgent like everyone else is talking about, so I had to take another route that may help some people.

    Instead of looking at the navigator.userAgent, I looked at navigator.appName to distinguish if it was IE<=10 or IE11 and Edge. IE11 and Edge use the appName of "Netscape", while every other iteration uses "Microsoft Internet Explorer".

    After we determine that the browser is either IE11 or Edge, I then looked to navigator.appVersion. I noticed that in IE11 the string was rather long with a lot of information inside of it. I arbitrarily picked out the word "Trident", which is definitely not in the navigator.appVersion for Edge. Testing for this word allowed me to distinguish the two.

    Below is a function that will return a numerical value of which Internet Explorer the user is on. If on Microsoft Edge it returns the number 12.

    Good luck and I hope this helps!

    function Check_Version(){
        var rv = -1; // Return value assumes failure.
    
        if (navigator.appName == 'Microsoft Internet Explorer'){
    
           var ua = navigator.userAgent,
               re  = new RegExp("MSIE ([0-9]{1,}[\\.0-9]{0,})");
    
           if (re.exec(ua) !== null){
             rv = parseFloat( RegExp.$1 );
           }
        }
        else if(navigator.appName == "Netscape"){                       
           /// in IE 11 the navigator.appVersion says 'trident'
           /// in Edge the navigator.appVersion does not say trident
           if(navigator.appVersion.indexOf('Trident') === -1) rv = 12;
           else rv = 11;
        }       
    
        return rv;          
    }
    
    0 讨论(0)
  • 2020-11-30 03:18

    Use this snip : var IE = (navigator.userAgent.indexOf("Edge") > -1 || navigator.userAgent.indexOf("Trident/7.0") > -1) ? true : false;

    0 讨论(0)
  • 2020-11-30 03:18

    First of all its not the Notepad++ problem for sure. Its your "String Matching problem"

    The common string throughout all IE version is MSIE Check out the various userAgent strings at http://www.useragentstring.com/pages/Internet%20Explorer/

    if(navigator.userAgent.indexOf("MSIE") != -1){
       alert('I am Internet Explorer!!');
    }
    
    0 讨论(0)
提交回复
热议问题