我需要检测 IE6 才能解决缺少position:fixed 的问题。我一直在使用一个简单的正则表达式:
var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
这几乎一直有效,除了浏览器声称同时是 IE6 和 IE7 的用户:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)
Glorious。
我很想使用 jquery.support,但看起来不支持查询position:fixed是否可用。所以我又开始检测IE6。
有各种建议的解决方案,例如查找 maxHeight 是否存在。但这些看起来相当随机,让我害怕——如果上面的正则表达式有例外,我如何确定 maxHeight 没有例外?
我正在考虑使用条件注释 - 这样至少 IE 本身就会声称是 IE6,而不是黑客。比如:
<!--[if IE 6]>
<SCRIPT> var isIE6 = true; </SCRIPT>
<![endif]-->
或者有一个功能功能可以直接测试position:fixed是否可用,但是好像有点重。
我的条件评论方法行不通有什么原因吗?有更好的方法吗?
I need to detect IE6 in order to work around the lack of position:fixed. I've been using a simple regex:
var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
This works almost all the time, except for the user whose browser claims to be both IE6 and IE7:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1; .NET CLR 3.0.04506.30)
Glorious.
I'd love to use jquery.support, but it looks like that doesn't support querying on whether position:fixed is available. So I'm back to detecting IE6.
There are various suggested solutions, such as looking for the existence of maxHeight. But those seem fairly random and scare me - if there are exceptions to the regex above, how can I be sure there are no exceptions to maxHeight?
I'm thinking of using conditional comments - that way at least it'll be IE itself claiming to be IE6, not a hack. Something like:
<!--[if IE 6]>
<SCRIPT> var isIE6 = true; </SCRIPT>
<![endif]-->
Alternatively there's a function that directly tests if position:fixed is available, but that seems a bit heavy.
Any reason my conditional comment approach won't work? Are there better approaches?
发布评论
评论(6)
但说真的,你的条件评论可能是最好、最准确的检测方法。即使浏览器位于其用户代理中,它也可能不会像 IE6 那样解析条件注释。
我不得不回家哭一会儿,因为我得知有人仍在为 IE6 进行开发。
Seriously though, your conditional comment is probably the best and most accurate detection method. Even if a browser lies in their user-agent, it presumably won't parse the conditional comment as if it were IE6.
I have to go home and cry a little bit now that I've learned someone is still developing for IE6.
Paul Irish 写道 对
$.support
< 的补充 /a> 专门用于检查position:fixed
支持。我建议您走这条路,尽可能使用功能检测而不是浏览器检测。您只需要包含该添加中的最后一个函数,这部分:
在 jQuery 之后包含此函数,然后您可以在代码中使用它,如下所示:
Paul Irish wrote an addition to
$.support
specifically for checking forposition: fixed
support. I recommend you go this route, using feature-detection rather than browser detection whenever possible.You just need to include the last function in that addition, this portion:
Include this after jQuery, then you can use it in your code, like this:
我遇到的解决 IE 问题的最佳方法是使用条件注释:
这种方法还将使您的页面向前兼容,以便未来版本的 IE 可以呈现它,而无需所有 IE6(及更低版本)样式。
The best way I've come across to overcome IE issues is to use conditional comments:
This approach will also make your page forward-compatible, so that future versions of IE can render it without needing all the IE6 (and lower) styles.
http://api.jquery.com/jQuery.browser/
http://api.jquery.com/jQuery.browser/
我说你也应该接受条件评论。它对于 CSS 和 JavaScript 或任何你可能想放在那里的东西都很有效。在我看来,这是最好的选择。但我不会像您在示例中那样使用变量。我会选择一个外部 ie6.js 链接,它将覆盖您在原始非 ie6 代码中所做的任何操作。这样,您就不会在干净的代码中获得 ie6 垃圾/变量。
I say you should go with the conditional comment too. It's working well for CSS and JavaScript or whatever you might want to put there. It seems to me like it's the best option. But I wouldn't use a variable like you did in your example. I would go for an external ie6.js link that will override whatever you are doing in your original non-ie6 code. That way, you will not get ie6 junk/variables in your clean code.
IE 博客上有一篇关于功能检测的精彩而详细的文章:
链接
基本上他们(可以理解)反对浏览器版本检测:
在很多情况下(如您的示例中),浏览器会谎报其版本,最好使用功能检测。
There is a nice and detailed post on IE blog on feature detection:
Link
Basically they're (understandably) against browser version detection:
There are so many scenarios (as in your example) that browsers lie about their versions, it's better to use feature detection instead.