如何使用 javascript 定位 Edge 浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31721250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to target Edge browser with javascript
提问by Willem de Wit
I know you should do feature detection where possible, but can you detect in Javascript if the browser is the Microsoft Edge browser?
我知道您应该在可能的情况下进行功能检测,但是如果浏览器是 Microsoft Edge 浏览器,您可以在 Javascript 中进行检测吗?
I maintain an old product and I want to display a warning that some features could be broken without having to invest a lot of time fixing the old code.
我维护一个旧产品,我想显示一个警告,即某些功能可能会被破坏,而不必投入大量时间修复旧代码。
回答by sandstrom
Try to detect features instead of a specific browser. It's more future-proof. Only rarely should you use browser detection.
尝试检测功能而不是特定浏览器。它更具前瞻性。很少使用浏览器检测。
With that out of the way: one option is to use a library (there are many intricacies to User Agent strings), or alternatively to parse window.navigator.userAgent
manually.
顺便说一句:一种选择是使用库(用户代理字符串有很多复杂性),或者window.navigator.userAgent
手动解析。
Using a parser library
使用解析器库
# https://github.com/faisalman/ua-parser-js.
var parser = new UAParser();
var result = parser.getResult();
var name = result.browser.name;
var version = result.browser.version;
Raw approach with Javascript
使用 Javascript 的原始方法
# Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) \
# Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136
window.navigator.userAgent.indexOf("Edge") > -1
回答by Manoj Kadolkar
Here's the simple script to detect Edge browser
这是检测Edge浏览器的简单脚本
if (/Edge/.test(navigator.userAgent)) {
alert('Hello Microsoft User!');
}
Explanation:
解释:
/Edge/
A regular expression to search for the string 'Edge' - which we then test against the 'navigator.userAgent' property
用于搜索字符串“Edge”的正则表达式 - 然后我们针对“navigator.userAgent”属性进行测试
回答by David Perfors
The useragent string contains Edge/12.9600, where the 12.9600 is the version number I tested with. This is completely different from the user agent string of Internet Explorer in 'Edge' mode.
用户代理字符串包含 Edge/12.9600,其中 12.9600 是我测试的版本号。这与 'Edge' 模式下 Internet Explorer 的用户代理字符串完全不同。
User agent string of Edge:
Edge 的用户代理字符串:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.9600
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.9600
User agent string of IE10 in Edge mode:
Edge模式下IE10的用户代理字符串:
Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; InfoPath.3; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; Tablet PC 2.0; rv:11.0) like Gecko
Mozilla/5.0(Windows NT 6.3;WOW64;Trident/7.0;.NET4.0E;.NET4.0C;InfoPath.3;.NET CLR 3.5.30729;.NET CLR 2.0.50727;.NET CLR 3.0.30729;Tablet PC 2.0; RV: 11.0) 像 Gecko
So when using javascript, just check for the word 'Edge' in the user agent string. When you also test for other browsers, make sure you check Edge first, otherwise you will get false positives (for example Chrome or Safari...)
因此,在使用 javascript 时,只需检查用户代理字符串中的“Edge”一词。当您还测试其他浏览器时,请确保先检查 Edge,否则您会得到误报(例如 Chrome 或 Safari...)
回答by cesaraviles
navigator.appVersion.indexOf('Edge') > -1
回答by Mark Cooper
You are not alone with this problem. At time of writing this post, even the Google Analytics does not exclusively identify the Edge browser.
遇到这个问题的不止您一个人。在撰写本文时,即使是 Google Analytics 也不能专门识别 Edge 浏览器。
Your best bet is to refer to the user agent of the request, it will be something like this:
最好的办法是引用请求的用户代理,它会是这样的:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.9600
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.9600
(To extract the user agent from the request in JavaScript see this post: Getting the User Agent with JavaScript)
(要从 JavaScript 请求中提取用户代理,请参阅这篇文章:使用 JavaScript 获取用户代理)
Edge is only available in Windows 10 so you can use this knowledge to help ensure your logic is safe. Look for the following values in the UA:
Edge 仅在 Windows 10 中可用,因此您可以使用这些知识来帮助确保您的逻辑安全。在 UA 中查找以下值:
Windows NT 10.0
- which tells you that the user is on Windows 10)Edge
- which tells you that the user is on Edge
Windows NT 10.0
- 告诉您用户使用的是 Windows 10)Edge
- 告诉您用户在 Edge 上
You could, of course, just look for Edge
too.
当然,你也可以只寻找Edge
。
Update - 05/08
更新 - 05/08
Google Analytics have now included Windows 10 and Edge as first class dimensions and these can both now be filtered directly.
Google Analytics 现在已将 Windows 10 和 Edge 作为第一类维度,现在可以直接过滤这些维度。
回答by Bhanu pratap
Browser checks using javascript
浏览器使用 javascript 检查
IsChrome: window.navigator.userAgent.indexOf("Chrome") > -1;
是铬: window.navigator.userAgent.indexOf("Chrome") > -1;
IsIE: window.navigator.userAgent.indexOf("MSIE ") > -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./);
是IE: window.navigator.userAgent.indexOf("MSIE ") > -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./);
IsEdge: window.navigator.userAgent.indexOf("Edge/") > -1;
IsEdge: window.navigator.userAgent.indexOf("Edge/") > -1;
IsSafari: window.navigator.userAgent.indexOf("Safari") > -1 && window.navigator.userAgent.indexOf('Chrome') == -1;
IsSafari: window.navigator.userAgent.indexOf("Safari") > -1 && window.navigator.userAgent.indexOf('Chrome') == -1;
IsFirefox: window.navigator.userAgent.indexOf("Firefox") > -1;
火狐浏览器: window.navigator.userAgent.indexOf("Firefox") > -1;
IsChromiumEdge: window.navigator.userAgent.indexOf("Edg/") > -1; // for new edge chromium
IsChromiumEdge: window.navigator.userAgent.indexOf("Edg/") > -1; // for new edge chromium
回答by Meily Chhon
The snippet that I have here is also copy from SO too, am sorry that I could not give you reference for the original code, but I have modified it so here it is for those of you looking for snippet to check for IE 11 and MS Edge Window 10:
我这里的代码片段也是从 SO 复制的,很抱歉,我无法为您提供原始代码的参考,但我已经修改了它,因此这里适合那些正在寻找代码片段以检查 IE 11 和 MS 的人边缘窗口 10:
var get_ie_version = function () {
var sAgent = window.navigator.userAgent;
var Idx = sAgent.indexOf("MSIE");
// If IE, return version number.
if (Idx > 0) {
return parseInt(sAgent.substring(Idx+ 5, sAgent.indexOf(".", Idx)));
}
// Condition Check IF IE 11 and or MS Edge
else if ( !!navigator.userAgent.match(/Trident\/7\./)
|| window.navigator.userAgent.indexOf("Edge") > -1 )
{
return 11;
} else {
return 0; //It is not IE
}
};
回答by Martin Fischer
https://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx
https://msdn.microsoft.com/en-us/library/ms537509%28v=vs.85%29.aspx
Have a try with:
试试看:
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp. );
}
return rv;
}
function checkVersion()
{
var msg = "You're not using Internet Explorer.";
var ver = getInternetExplorerVersion();
if ( ver > -1 )
{
if ( ver >= 8.0 )
msg = "You're using a recent copy of Internet Explorer."
else
msg = "You should upgrade your copy of Internet Explorer.";
}
alert( msg );
}
Replace the String Microsoft Internet Explorer
with maybe something of Edge
or similar.
Microsoft Internet Explorer
用可能的Edge
或类似的东西替换字符串。
EDIT:You can find out what the user agent string is with:
编辑:您可以找出用户代理字符串是什么:
alert(navigator.userAgent)
回答by NinjaKC
Everyone seems to be saying the same thing here, except no one has provided a solid 1 liner solution.
在这里,每个人似乎都在说同样的话,只是没有人提供可靠的 1 衬里解决方案。
So from this answer, https://stackoverflow.com/a/32107845/584147
所以从这个答案,https://stackoverflow.com/a/32107845/584147
Which simply says that,
简单地说,
- Both of these browsers (IE 11 & Edge) use the navigator.appName of "Netscape"(compared to older versions of IE which use "Microsoft Internet Explorer" as the navigator.appName)
- in IE 11 the navigator.appVersion says 'trident', in Edge the navigator.appVersion does not say trident
- 这两种浏览器(IE 11 和 Edge)都使用“Netscape”的 navigator.appName(与使用“Microsoft Internet Explorer”作为 navigator.appName 的旧版 IE 相比)
- 在 IE 11 中,navigator.appVersion 表示“三叉戟”,在 Edge 中,navigator.appVersion 不表示三叉戟
I have turned this into a simple 1 liner for anyone else who ends up here and in need of it.
我已经把它变成了一个简单的 1 衬里,供任何最终来到这里并需要它的人使用。
var isieEdge = (navigator.appName == "Netscape") && (navigator.appVersion.indexOf('Trident') === -1); // IE Edge
alert(isieEdge);