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

前端 未结 13 940
小鲜肉
小鲜肉 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: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();
    };
    

提交回复
热议问题