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

问题描述 投票:0回答:3

ArcGIS 正在弃用 IE11 和旧版 Edge 浏览器及其当前和未来版本的 JavaScript API。

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 仍然是用户代理中的边缘。我还可以使用其他任何东西来区分两者吗?

javascript microsoft-edge user-agent navigator arcgis-js-api
3个回答
1
投票

在 Edge Chromium 用户代理中为

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

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

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>

1
投票

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

  • 边缘=传统
  • Edg = 基于铬
  • EdgA = 安卓
  • EdgiOS = iOS

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

function isEdge(versionName, userAgent) {
    versionName = versionName ? versionName.toLowerCase() : "desktop"
    userAgent = userAgent ? userAgent : navigator.userAgent

    const options = {
        desktop: () => (/Edge?\//).test(userAgent),
        chromium: () => (/Edg\//).test(userAgent),
        legacy: () => (/Edge\//).test(userAgent),
        mobile: () => (/Edg(A|iOS)\//).test(userAgent),
        android: () => (/EdgA\//).test(userAgent),
        ios: () => (/EdgiOS\//).test(userAgent)
    }
    return options[versionName]()
}

使用示例:

// 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://learn.microsoft.com/pt-br/microsoft-edge/web-platform/user-agent-string

Edge 12 和 13 的用户代理有不同吗?

© www.soinside.com 2019 - 2024. All rights reserved.