How to target Edge browser with javascript

后端 未结 9 750
無奈伤痛
無奈伤痛 2020-12-05 09:26

I know you should do feature detection where possible, but can you detect in Javascript if the browser is the Microsoft Edge browser?

I maintain an old product and I

9条回答
  •  孤街浪徒
    2020-12-05 09:54

    Everyone seems to be saying the same thing here, except no one has provided a solid 1 liner solution.

    So from this answer, https://stackoverflow.com/a/32107845/584147

    Which simply says that,

    1. Both of these browsers (IE 11 & Edge) use the navigator.appName of "Netscape" (compared to older versions of IE which use "Microsoft Internet Explorer" as the navigator.appName)
    2. in IE 11 the navigator.appVersion says 'trident', in Edge the navigator.appVersion does not say trident

    I have turned this into a simple 1 liner for anyone else who ends up here and in need of it.

    var isieEdge = (navigator.appName == "Netscape") && (navigator.appVersion.indexOf('Trident') === -1); // IE Edge
    
    alert(isieEdge);
    

提交回复
热议问题