如何使用JavaScript定位Edge浏览器 [英] How to target Edge browser with javascript
问题描述
我知道您应该尽可能进行特征检测,但是如果浏览器是Microsoft Edge浏览器,是否可以使用Javascript进行检测?
I know you should do feature detection where possible, but can you detect in Javascript if the browser is the Microsoft Edge browser?
我维护的是旧产品,但我想显示警告,指出某些功能可能会被破坏,而不必花费大量时间来修复旧代码.
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.
推荐答案
尝试检测功能而不是特定的浏览器.更具前瞻性.很少使用浏览器检测.
顺便说一句:一种选择是使用库(用户代理字符串有很多复杂性),或者手动解析window.navigator.userAgent
.
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.
使用解析器库
# 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;
使用Java的原始方法
# 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
这篇关于如何使用JavaScript定位Edge浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!