Detecting Edge Legacy browser, not Edge Chromium

Viewed 1965

ArcGIS is depreciating use of IE11 and Legacy Edge browsers with current and future versions of their JavaScript API.

https://www.esri.com/arcgis-blog/products/arcgis/announcements/so-long-internet-explorer-11/

I'd like to know how to target legacy version of edge and alert users things may not work properly. Using:

navigator.userAgent.indexOf("Edge/") > -1 || navigator.userAgent.indexOf("Edg/") > -1;

does not work since Chromium Edge is still edge in the user agent. Is there anything else I could use that would allow me to differentiate between the two?

3 Answers

It's Edg in Edge Chromium user agent and Edge in Edge Legacy user agent. So you only need to use Edge to detect Edge Legacy:

navigator.userAgent.indexOf("Edge/") > -1

You could use the window.navigator userAgent to check whether the browser is Microsoft Chromium Edge or Chrome.

Code as below:

<script>
    var browser = (function (agent) {
        switch (true) {
            case agent.indexOf("edge") > -1: return "edge";
            case agent.indexOf("edg/") > -1: return "chromium based edge (dev or canary)"; // Match also / to avoid matching for the older Edge
            case agent.indexOf("opr") > -1 && !!window.opr: return "opera";
            case agent.indexOf("chrome") > -1 && !!window.chrome: return "chrome";
            case agent.indexOf("trident") > -1: return "ie";
            case agent.indexOf("firefox") > -1: return "firefox";
            case agent.indexOf("safari") > -1: return "safari";
            default: return "other";
        }
    })(window.navigator.userAgent.toLowerCase());
    document.body.innerHTML = window.navigator.userAgent.toLowerCase() + "<br>" + browser;
</script>

All versions of Edge have the word "Edg" in the userAgent followed by X and ending with "/", where X:

  • Edge = Legacy
  • Edg = Based on chromium
  • EdgA = Android
  • EdgiOS = iOS

therefore, just a few regular expressions are enough to identify the desired cases:

// is-edge.js v1
function isEdge(versionName, userAgent){
    versionName = versionName ? versionName.toLowerCase() : 'desktop';
    userAgent = userAgent ? userAgent : navigator.userAgent;
    switch(versionName){
        case 'desktop':
            return (/Edge?\//).test(userAgent);
        case 'chromium':
            return (/Edg\//).test(userAgent);
        case 'legacy':
            return (/Edge\//).test(userAgent);
        case 'mobile':
            return (/Edg(A|iOS)\//).test(userAgent);
        case 'android':
            return (/EdgA\//).test(userAgent);
        case 'ios':
            return (/EdgiOS\//).test(userAgent);
    };
};

Example of use:

// checks if the browser is edge on a desktop (just windows, right?)
isEdge('desktop'); // boolean
isEdge(); // boolean (empty = 'desktop')

// check if the browser is the edge and the version is based on chromium (version with the blue and green logo with waveform)
isEdge('chromium'); // boolean

// check if the browser is the edge and the version is legacy (version with blue logo similar to internet explorer)
isEdge('legacy'); // boolean

// check if the browser is the edge for android OR iOS
isEdge('mobile'); // boolean

// check if the browser is the edge for android
isEdge('android'); // boolean

// check if the browser is the edge for iOS
isEdge('ios'); // boolean

https://docs.microsoft.com/pt-br/microsoft-edge/web-platform/user-agent-string

Do the user agents of Edge 12 and 13 differ?

Related