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

前端 未结 13 949
小鲜肉
小鲜肉 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:21

    Topic is a bit old, but since the scripts here detect Firefox as a False Positive (EDGE v12), here is the version I use:

    function isIEorEDGE(){
      if (navigator.appName == 'Microsoft Internet Explorer'){
        return true; // IE
      }
      else if(navigator.appName == "Netscape"){                       
         return navigator.userAgent.indexOf('.NET') > -1; // Only Edge uses .NET libraries
      }       
    
      return false;
    }
    

    which of course can be written in a more concise way:

    function isIEorEDGE(){
      return navigator.appName == 'Microsoft Internet Explorer' || (navigator.appName == "Netscape" && navigator.userAgent.indexOf('.NET') > -1);
    }
    

提交回复
热议问题