我环顾四周,了解到有很多方法可以检测 Internet Explorer。
我的问题是这样的:我的 HTML 文档上有一个区域,单击该区域时,会调用与任何类型的 Internet Explorer 都不兼容的 JavaScript 函数。
我想检测是否正在使用 IE,如果是,则将变量设置为 true。
问题是,我正在用 Notepad++ 编写代码,当我在浏览器中运行 HTML 代码时,检测 IE 的方法都不起作用。我认为问题是我正在用 Notepad++ 运行它。我需要能够检测 IE,以便根据变量,我可以禁用网站的该区域。我已经尝试过这个:
var isIE10 = false;
if (navigator.userAgent.indexOf("MSIE 10") > -1) {
// this is internet explorer 10
isIE10 = true;
window.alert(isIE10);
}
var isIE = (navigator.userAgent.indexOf("MSIE") != -1);
if(isIE){
if(!isIE10){
window.location = 'pages/core/ie.htm';
}
}
但它不起作用。如何从 Notepad++ 中检测 IE?这就是我正在测试 HTML 的内容,但我需要一种可以使用它的方法。
edit
我注意到有人将此标记为重复,这是可以理解的。我想我没说清楚。我不能使用 JQuery 答案,所以这不是重复的,因为我要求一个普通的 JS 答案。
Edit #2
还有办法检测 Microsoft Edge 浏览器吗?
这是我所知道的检查 IE 和 Edge 的最新正确方法:
if (/MSIE 10/i.test(navigator.userAgent)) {
// This is internet explorer 10
window.alert('isIE10');
}
if (/MSIE 9/i.test(navigator.userAgent) || /rv:11.0/i.test(navigator.userAgent)) {
// This is internet explorer 9 or 11
window.location = 'pages/core/ie.htm';
}
if (/Edge\/\d./i.test(navigator.userAgent)){
// This is Microsoft Edge
window.alert('Microsoft Edge');
}
请注意,您的代码中不需要额外的 var isIE10,因为它现在执行非常具体的检查。
另请查看此页面以获取最新的 IE 和 Edge 用户代理字符串,因为此答案可能会在某些时候变得过时:https://msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx https://msdn.microsoft.com/en-us/library/hh869301%28v=vs.85%29.aspx
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)