javascript 如何检测 Internet Explorer 11 及以下版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49986720/
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 detect Internet Explorer 11 and below versions?
提问by Ryan Wilson
I'm wondering how I could detect if the user viewing my website is using Internet Explorer 11 or below versions with Javascript.
我想知道如何检测查看我网站的用户是否使用带有 Javascript 的 Internet Explorer 11 或更低版本。
It should be compatible and works with all these versions.
它应该兼容并适用于所有这些版本。
How to achieve that ?
如何做到这一点?
回答by Ryan Wilson
Here you go, this should work for you:
给你,这应该适合你:
//Per Icycool, one liner
//function isIE(){
// return window.navigator.userAgent.match(/(MSIE|Trident)/);
// }
function isIE() {
const ua = window.navigator.userAgent; //Check the userAgent property of the window.navigator object
const msie = ua.indexOf('MSIE '); // IE 10 or older
const trident = ua.indexOf('Trident/'); //IE 11
return (msie > 0 || trident > 0);
}
//function to show alert if it's IE
function ShowIEAlert(){
if(isIE()){
alert("User is using IE");
}
}
回答by Timm
I'd like to streamline the correct answer. This returns trueor false:
我想简化正确的答案。这将返回true或false:
function is_IE() {
return (window.navigator.userAgent.match(/MSIE|Trident/) !== null);
}

