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 仍然是用户代理中的边缘。我还可以使用其他任何东西来区分两者吗?
在 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:
因此,只需几个正则表达式就足以识别所需的情况:
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