检测任何和所有版本的Internet Explorer
Detecting any and all versions of Internet Explorer
曾经有一种很好的方法来判断web浏览器是否是IE,通过在HTML中使用这种技术:
<!--[if IE]>
Non-IE browsers ignore this
<![endif]-->
或
<!--[if !IE]-->
IE ignores this
<!--[endif]-->
但是这个在IE 10中不再工作了。
任何想法使用什么来告诉IE从其他web浏览器(使用HTML或JavaScript)?
p。我需要能够告诉任何版本的IE 从非IE网页浏览器。
我很欣赏你的见解,但是没有一个能回答我真正的问题。再次强调,我不是在问特征检测。所有我需要知道的是,如果网络浏览器是IE或不是。以下代码使用JavaScript,似乎适用于所有当前版本的IE(包括IE10):
<![if IE]>
<script type='text/javascript'>
if(/*@cc_on!@*/false)
var bIsIE = 1;
</script>
<![endif]>
然后检查它是否为IE,从JavaScript中执行:
if (typeof (bIsIE) != 'undefined')
{
//The web browser is IE
}
else
{
//The web browser is not IE
}
显然,上面的代码假设web浏览器启用了JavaScript。但在我的情况下,浏览器检测是相关的,只有当它启用了脚本。
每个版本的Internet Explorer都是不同的,就像每个版本的Chrome、Firefox和Opera都不同于它们的前身一样。当你开发网站时,你的目标不是像"Microsoft"、"Google"或"Mozilla"这样的供应商,你的目标是功能。
而不是问"我想使用::后,这个浏览器是微软浏览器吗?你应该问"这个浏览器是否支持::前缀上的伪元素?""这是特征检测,它几乎总是完美地瞄准目标。你可以通过浏览器实际能做什么来判断它的能力,而不是猜测它的供应商能做什么。
这可能不是你想要的答案,但这是正确的答案。如果你问如何识别所有的Microsoft浏览器,你正在错误地处理问题(或者你认为是一个问题)。
对于合适的解决方案,我建议您使用jQuery和Modernizr等工具。它们将处理API规范化、在旧浏览器中添加新元素以及特性检测。这是正确的处理事情的方法,如果开发人员从一开始就采用这种方法,那么今天您可能不会对Internet Explorer感到如此厌恶。
您在问题中给出的链接-不再工作-这是Windows Internet Explorer工程团队博客导致以下声明
条件评论<!--[if IE]> This content is ignored in IE10 and other browsers. In older versions of IE it renders as part of the page. <![endif]-->
这意味着条件注释仍然可以使用,但只能使用针对旧版本的IE。如果你需要区分更多最近的浏览器,使用特征检测代替。
在我看来,IE团队似乎正在强烈推动使用功能检测而不是浏览器检测,正如上面的功能检测链接所示。
相同的标记:核心指南
**DO** Feature Detection Test whether a browser supports a feature before using it. Behavior Detection Test for known issues before applying a workaround. **DON'T** Detect Specific Browsers Also known as browser detection. Don't use the identity of a browser (e.g. navigator.userAgent) to alter page behavior. Assume Unrelated Features Don't perform feature detection for one feature, and then proceed to use a different feature.
看来Windows Internet Explorer工程团队正在设置IE,因此您将无法使用IE10及以上版本的浏览器检测。
编辑我不使用IE10,但使用
navigator.appName=='Microsoft Internet Explorer';
在IE10中工作吗?
仅仅说IE10足够好而忽略问题是不够的。这取决于你想要做什么。在大多数情况下,特征检测可能会处理你需要的东西。更复杂的路由是通过从HTTP请求头中提取用户代理字符串来启动用户代理检测。如果你不小心,你可能很快就会出错。
在浏览器JS控制台中查看当前用户代理字符串:
console.log(navigator.userAgent);
下面是所有浏览器中报告的用户代理字符串列表:
http://www.zytrax.com/tech/web/browser_ids.htm请注意,所有的MS Explorer代理字符串都将包含"MSIE",但首先你必须清除像Opera这样在某些情况下也包含"MSIE"字符串的浏览器。
如果客户端浏览器为Internet Explorer,则此函数返回true
,测试版本为9-10-11。
function isIE(v) {
var ie;
ie = RegExp('msie' + (!isNaN(v)?('''s'+v):''), 'i').test(navigator.userAgent);
if (!ie) { ie = !!navigator.userAgent.match(/Trident.*rv[ :]*11'./) }
return ie;
}
// Example
var ie = isIE();
var ie9 = isIE(9);
var ie10 = isIE(10);
注意:功能不完整,不允许isIE(11)
- 视频HTML没有'无法在Internet Explorer 11上工作
- Internet Explorer缺少占位符支持,特别是密码字段
- Internet Explorer中的文本换行错误
- 为什么我在Internet Explorer上看不到html元素
- internet explorer 8兼容模式下的Youtube api错误
- 当浏览器控制台未打开时,为什么要求会导致Internet Explorer 9超时
- Internet Explorer 8. Events
- jQuery表单验证适用于Mozilla和Internet Explorer,但不适用于Chrome或Safari
- element.dataset in Internet Explorer
- Facebook FB.init”;访问被拒绝”;在Internet Explorer 11中
- JavaScript不适用于internet explorer和Firefox,但适用于谷歌
- 在Internet Explorer中,向所选内容添加选项不起作用
- 在Internet Explorer中从二进制文件打开PDF
- elementproblem internet explorer
- 我可以使用哪些方法在Internet Explorer中禁用Alt+*X*
- 如何在Internet Explorer中使用javascript设置cookie
- 只有当alert()在带有AJAX的internet explorer中使用时,Javascript才会更新UI
- sharepoint站点的internet explorer中出现Javascript错误
- Internet Explorer重新加载IFrame
- 如何在不使用Internet Explorer的情况下从未挂起的HTML文件执行程序