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

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

    // detect IE8 and above, and Edge
    if (document.documentMode || /Edge/.test(navigator.userAgent)) {
        ... do something
    }
    

    Explanation:

    document.documentMode
    

    An IE only property, first available in IE8.

    /Edge/
    

    A regular expression to search for the string 'Edge' - which we then test against the 'navigator.userAgent' property

    Update Mar 2020

    @Jam comments that the latest version of Edge now reports Edg as the user agent. So the check would be:

    if (document.documentMode || /Edge/.test(navigator.userAgent) || /Edg/.test(navigator.userAgent)) {
        ... do something
    }
    

提交回复
热议问题