How to detect IE7 and IE8 using jQuery.support

2020-02-02 11:33发布

How can I detect IE 7 and IE 8 using jQuery.support properties?

Is it possible to detect the browser versions using jQuery.support or just those blah blah blah browser features?

12条回答
啃猪蹄的小仙女
2楼-- · 2020-02-02 12:11

as explained, .support is for feature-detection. if you want to detect the browser, just use .browser.

 var ua = $.browser;
 if ( ua.msie && ua.version.slice(0,1) == "8" ) {
   alert('IE 8');
 } else if ( ua.msie && ua.version.slice(0,1) == "7" ) {
   alert('IE 7');
 } else {
   alert('something else');
 }
查看更多
做自己的国王
3楼-- · 2020-02-02 12:17

This is totally possible with support:

if (!$.support.leadingWhitespace) {
    //IE7 and 8 stuff
}

This also detects IE 6 however, if you don't want IE 6 to run this code block, you will need another flag to exclude it

A good reason for not using browser is it is a deprecated feature, and it will likely be removed to a plugin with version 1.9. (See the comments on the answer to How to detect IE7 with jQuery?)

"We recommend against using this property; please try to use feature detection instead (see jQuery.support). jQuery.browser may be moved to a plugin in a future release of jQuery" http://api.jquery.com/jQuery.browser/

here is a working example: http://jsfiddle.net/AGtG8/16/

查看更多
女痞
4楼-- · 2020-02-02 12:17

I use this to check if browser is ie 8 or less

to change the ie version simply update the 8 to another ie verion

if (jQuery.browser.msie && jQuery.browser.version.substr(0,1) <= 8 ) {   
    runIECode();
} else {
    runOther();
}
查看更多
老娘就宠你
5楼-- · 2020-02-02 12:22

jQuery.support is for detecting browser features. To detect browser version use jQuery.browser:

if ($.browser.msie && $.browser.version.substr(0,1)<7) {
//IE7

}

Update: $.browser is deprecated now, don't use it.

查看更多
闹够了就滚
6楼-- · 2020-02-02 12:24

Be careful with the following because it also includes IE10:

if ($.browser.msie && $.browser.version.substr(0,1)<7) {
//<IE7
}

better use:

if ($.browser.msie && parseInt($.browser.version,10)<7) {
//<IE7
}
查看更多
Animai°情兽
7楼-- · 2020-02-02 12:24

One trick I found is to add Conditionnal Tags in the HTML

<!--[if lt IE 7]><body class="ie ie6 lte9 lte8 lte7"><![endif]-->
<!--[if IE 7]><body class="ie ie7 lte9 lte8 lte7"><![endif]-->
<!--[if IE 8]><body class="ie ie8 lte9 lte8"><![endif]-->
<!--[if IE 9]><body class="ie ie9 lte9"><![endif]-->
<!--[if !IE]><!--><body class="not-ie"><!--<![endif]-->

Then use JQuery like this :

$('body.ie7')

It also helps for CSS on IE specific

body.ie6{ margin: 0; }

I don't know if it's good for you but anyways, that's still an option.

查看更多
登录 后发表回答