使用Javascript/jQuery重新检测IE6

Par*_*and 3 javascript css jquery internet-explorer-6 browser-feature-detection

我需要检测IE6才能解决缺少位置的问题:修复.我一直在使用一个简单的正则表达式:

var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
Run Code Online (Sandbox Code Playgroud)

除了浏览器声称同时属于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)
Run Code Online (Sandbox Code Playgroud)

辉煌.

我喜欢使用jquery.support,但看起来它不支持查询position:fixed是否可用.所以我回来检测IE6.

有各种建议的解决方案,例如寻找maxHeight的存在.但那些似乎相当随意并吓到我 - 如果上面的正则表达式有例外,我怎么能确定maxHeight没有例外?

我正在考虑使用条件评论 - 至少它是IE本身声称是IE6,而不是黑客.就像是:

<!--[if IE 6]>
<SCRIPT> var isIE6 = true; </SCRIPT>
<![endif]-->
Run Code Online (Sandbox Code Playgroud)

或者,可以直接测试position:fixed是否可用,但这看起来有点沉重.

我的条件评论方法不起作用的任何原因?有更好的方法吗?

Set*_*eth 6

<script type="text/javascript">
    if (nothing_works) {
        is_ie6 = true;
    }
</script>
Run Code Online (Sandbox Code Playgroud)

但严重的是,您的条件评论可能是最好,最准确的检测方法.即使浏览器位于其用户代理中,也可能不会将条件注释解析为IE6.

我已经知道有人还在为IE6开发,我必须回家哭一点.


Nic*_*ver 5

保罗爱尔兰的除了$.support专门用于检查position: fixed的支持.我建议您尽可能使用功能检测而不是浏览器检测.

你只需要在该添加中包含最后一个函数,这部分:

$.support.positionFixed = (function() { ..... })();
Run Code Online (Sandbox Code Playgroud)

在jQuery之后包含这个,然后你可以在你的代码中使用它,如下所示:

if(!$.support.positionFixed) {
  //handle browsers that don't support it
}
Run Code Online (Sandbox Code Playgroud)