How to target Edge browser with javascript

后端 未结 9 732
無奈伤痛
無奈伤痛 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);
    
    0 讨论(0)
  • 2020-12-05 09:56

    https://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx

    Have a try with:

    function getInternetExplorerVersion()
    // Returns the version of Internet Explorer or a -1
    // (indicating the use of another browser).
    {
      var rv = -1; // Return value assumes failure.
      if (navigator.appName == 'Microsoft Internet Explorer')
      {
        var ua = navigator.userAgent;
        var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
          rv = parseFloat( RegExp.$1 );
      }
      return rv;
    }
    function checkVersion()
    {
      var msg = "You're not using Internet Explorer.";
      var ver = getInternetExplorerVersion();
    
      if ( ver > -1 )
      {
        if ( ver >= 8.0 ) 
          msg = "You're using a recent copy of Internet Explorer."
        else
          msg = "You should upgrade your copy of Internet Explorer.";
      }
      alert( msg );
    }
    

    Replace the String Microsoft Internet Explorer with maybe something of Edge or similar.

    EDIT: You can find out what the user agent string is with:

    alert(navigator.userAgent)
    
    0 讨论(0)
  • 2020-12-05 09:59

    Browser checks using javascript

    IsChrome: window.navigator.userAgent.indexOf("Chrome") > -1;

    IsIE: window.navigator.userAgent.indexOf("MSIE ") > -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./);

    IsEdge: window.navigator.userAgent.indexOf("Edge/") > -1;

    IsSafari: window.navigator.userAgent.indexOf("Safari") > -1 && window.navigator.userAgent.indexOf('Chrome') == -1;

    IsFirefox: window.navigator.userAgent.indexOf("Firefox") > -1;

    IsChromiumEdge: window.navigator.userAgent.indexOf("Edg/") > -1; // for new edge chromium

    0 讨论(0)
  • 2020-12-05 10:00

    You are not alone with this problem. At time of writing this post, even the Google Analytics does not exclusively identify the Edge browser.

    Your best bet is to refer to the user agent of the request, it will be something like this:

    Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.9600

    (To extract the user agent from the request in JavaScript see this post: Getting the User Agent with JavaScript)

    Edge is only available in Windows 10 so you can use this knowledge to help ensure your logic is safe. Look for the following values in the UA:

    • Windows NT 10.0 - which tells you that the user is on Windows 10)
    • Edge - which tells you that the user is on Edge

    You could, of course, just look for Edge too.

    Update - 05/08

    Google Analytics have now included Windows 10 and Edge as first class dimensions and these can both now be filtered directly.

    0 讨论(0)
  • 2020-12-05 10:03

    Here's the simple script to detect Edge browser

    if (/Edge/.test(navigator.userAgent)) {
        alert('Hello Microsoft User!');
    }
    

    Explanation:

    /Edge/
    

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

    0 讨论(0)
  • 2020-12-05 10:04
    navigator.appVersion.indexOf('Edge') > -1
    
    0 讨论(0)
提交回复
热议问题