检测 Edge Legacy 浏览器,而不是 Edge Chromium

Detecting Edge Legacy browser, not Edge Chromium

ArcGIS 在当前和未来版本的 JavaScript API.

中弃用 IE11 和 Legacy Edge 浏览器

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

我想知道如何针对边缘的旧版本并提醒用户可能无法正常工作。使用:

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

不起作用,因为 Chromium Edge 在用户代理中仍然是边缘。还有什么我可以使用的东西可以让我区分这两者吗?

在 Edge Chromium 用户代理中是 Edg,在 Edge Legacy 用户代理中是 Edge。所以你只需要使用Edge来检测Edge Legacy:

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

您可以使用 window.navigator userAgent 来检查浏览器是 Microsoft Chromium Edge 还是 Chrome。

代码如下:

<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>

所有版本的 Edge 在 userAgent 中都有单词“Edg”,后跟 X 并以“/”结尾,其中 X:

  • 边缘=传统
  • Edge = 基于铬
  • EdgA = Android
  • 埃德iOS = iOS

因此,只需几个正则表达式就足以识别所需的情况:

// 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);
    };
};

使用示例:

// 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