Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
93 views
in Technique[技术] by (71.8m points)

How to target Edge browser with 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.

question from:https://stackoverflow.com/questions/31721250/how-to-target-edge-browser-with-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

# 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...