How to target Edge browser with javascript

后端 未结 9 743
無奈伤痛
無奈伤痛 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: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)
    

提交回复
热议问题