gpt4 book ai didi

javascript - 如何使用 JavaScript 检测 Internet Explorer (IE) 和 Microsoft Edge?

转载 作者:IT王子 更新时间:2023-10-29 02:51:06 26 4
gpt4 key购买 nike

我看了很多地方,我知道有很多方法可以检测 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 的内容,但我需要一种适用于它的方法。

编辑

我注意到有人将其标记为重复,这是可以理解的。我想我不清楚。我不能使用 JQuery 答案,所以这不是重复的,因为我要求的是普通 JS 答案。

编辑 #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

关于javascript - 如何使用 JavaScript 检测 Internet Explorer (IE) 和 Microsoft Edge?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31757852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com