Javascript 如何检测 IE 和 Edge 浏览器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33152523/
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 do I detect IE and Edge browser?
提问by Brent Nicolet
Can't get Parallax working properly in IE or Microsoft Edge. I've looked in forums and haven't found a solution to the problem. I've come up with hopefully a solution for now. I want to make a message appear if the user is using IE or Edge. Not sure how I can detect that the browser being used is one or the either.
无法让视差在 IE 或 Microsoft Edge 中正常工作。我查看了论坛,但没有找到解决问题的方法。我已经提出了希望现在的解决方案。如果用户使用 IE 或 Edge,我想显示一条消息。不确定我如何检测正在使用的浏览器是其中之一。
Here is some javascript code I'm trying to work with:
这是我正在尝试使用的一些 javascript 代码:
<script src="https://github.com/ded/bowser/blob/master/src/bowser.js"></script>
// Determine Browser Used
browser = require('bowser').browser; becomes browser = require('bowser');
if (bowser.msie || bowser.msedge) {
alert('Hello Microsoft User');
}
Any help would be appreciated or if there is a better solution.
任何帮助将不胜感激,或者如果有更好的解决方案。
回答by Reda
I doubt you really need to detect the browser. But here it is anyway (don't really need to use a library):
我怀疑你真的需要检测浏览器。但无论如何都在这里(并不真的需要使用库):
// detect IE8 and above, and edge
if (document.documentMode || /Edge/.test(navigator.userAgent)) {
alert('Hello Microsoft User!');
}
回答by James Peter
For me better this:
对我来说更好的是:
var uA = window.navigator.userAgent,
isIE = /msie\s|trident\/|edge\//i.test(uA) && !!(document.uniqueID || document.documentMode || window.ActiveXObject || window.MSInputMethodContext),
checkVersion = (isIE && +(/(edge\/|rv:|msie\s)([\d.]+)/i.exec(uA)[2])) || NaN;