最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Detecting IE6 using JavascriptjQuery revisited - Stack Overflow

programmeradmin1浏览0评论

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 (patible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (patible; 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 ments - 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 ment approach won't work? Are there better approaches?

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 (patible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (patible; 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 ments - 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 ment approach won't work? Are there better approaches?

Share Improve this question edited May 2, 2015 at 1:46 Sam Hanley 4,7557 gold badges38 silver badges64 bronze badges asked Aug 5, 2010 at 21:40 ParandParand 106k49 gold badges156 silver badges188 bronze badges 1
  • 3 I've been using the conditional ment approach for a while now and haven't had any problems with it. – Pat Commented Aug 5, 2010 at 21:45
Add a ment  | 

6 Answers 6

Reset to default 6
<script type="text/javascript">
    if (nothing_works) {
        is_ie6 = true;
    }
</script>

Seriously though, your conditional ment 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 ment 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 wrote an addition to $.support specifically for checking for position: fixed support. I remend 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:

$.support.positionFixed = (function() { ..... })();

Include this after jQuery, then you can use it in your code, like this:

if(!$.support.positionFixed) {
  //handle browsers that don't support it
}

The best way I've e across to overe IE issues is to use conditional ments:

<!--[if IE 6]>
... link IE 6 specific stylesheet or a script...
<![endif]-->

This approach will also make your page forward-patible, so that future versions of IE can render it without needing all the IE6 (and lower) styles.

http://api.jquery./jQuery.browser/

if ($.browser.msie && parseInt($.browser.version) == 6) {
  // do something
}

I say you should go with the conditional ment 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.

<!--[if IE 6]>
<script type="text/javascript" src="ie6.js"></script>
<![endif]-->

There is a nice and detailed post on IE blog on feature detection:

Link

Basically they're (understandably) against browser version detection:

"DON'T: Detect Specific Browsers"

There are so many scenarios (as in your example) that browsers lie about their versions, it's better to use feature detection instead.

发布评论

评论列表(0)

  1. 暂无评论