【问题标题】:Detecting Edge Legacy browser, not Edge Chromium检测 Edge Legacy 浏览器,而不是 Edge Chromium
【发布时间】:2020-12-11 22:48:18
【问题描述】:

ArcGIS 正在弃用 IE11 和 Legacy 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 在用户代理中仍然是边缘。还有什么我可以用来区分这两者的吗?

【问题讨论】:

  • 如果您从 ArcGIS 嵌入某些内容,ArcGIS 肯定会处理错误通知吗?

标签: javascript microsoft-edge user-agent navigator arcgis-js-api


【解决方案1】:

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

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

【讨论】:

  • 啊,那是我读错了。我没有意识到删除 e 是他们区分铬版本的方式。谢谢!
【解决方案2】:

您可以使用 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>

【讨论】:

  • 感谢您列出其他浏览器!
  • 这就是你要找的东西吗?
  • 我真的只需要 Legacy Edge 的正确用户代理代码。 Yu的回答对我的特定问题更简洁。
【解决方案3】:

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

  • Edge = 旧版
  • Edg = 基于铬
  • EdgA = 安卓
  • EdgiOS = 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

Do the user agents of Edge 12 and 13 differ?

【讨论】:

    猜你喜欢
    • 2020-09-21
    • 2020-06-21
    • 2020-10-10
    • 1970-01-01
    • 2020-05-03
    • 2016-01-14
    • 2015-10-30
    • 2020-03-28
    • 1970-01-01
    相关资源
    最近更新 更多