How can I target only Internet Explorer 11 with JavaScript?

后端 未结 7 2341
一个人的身影
一个人的身影 2020-11-27 03:23

What\'s the least error-prone way to target just IE11 with JavaScript?

Note: This should really only be done for analytics or informing the user what browser they\'r

7条回答
  •  無奈伤痛
    2020-11-27 03:41

    IE11 keeps "Trident" in it's UA string, but drops MSIE. A simple way to detect the browser is IE11 or above (IE12, IE13, etc) is:

    var isAtLeastIE11 = !!(navigator.userAgent.match(/Trident/) && !navigator.userAgent.match(/MSIE/));
    

    If you want just IE11 (and you don't want future versions of IE to match), do this:

    var isIE11 = !!(navigator.userAgent.match(/Trident/) && navigator.userAgent.match(/rv[ :]11/));
    

提交回复
热议问题