How to detect IE7 and IE8 using jQuery.support

前端 未结 12 1236
忘掉有多难
忘掉有多难 2020-12-08 10:46

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 brows

相关标签:
12条回答
  • 2020-12-08 10:50

    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();
    }
    
    0 讨论(0)
  • 2020-12-08 10:51

    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.

    0 讨论(0)
  • 2020-12-08 10:59

    I added few lines of Javascript to make it work again:

    jQuery.uaMatch = function (ua) {
        ua = ua.toLowerCase();
    
        var match = /(chrome)[ \/]([\w.]+)/.exec(ua) ||
                /(webkit)[ \/]([\w.]+)/.exec(ua) ||
                /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) ||
                /(msie) ([\w.]+)/.exec(ua) ||
                ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) ||
                [];
    
        return {
            browser: match[ 1 ] || "",
            version: match[ 2 ] || "0"
        };
    };
    
    if (!jQuery.browser) {
        matched = jQuery.uaMatch(navigator.userAgent);
        browser = {};
    
        if (matched.browser) {
            browser[ matched.browser ] = true;
            browser.version = matched.version;
        }
    
        // Chrome is Webkit, but Webkit is also Safari.
        if (browser.chrome) {
            browser.webkit = true;
        } else if (browser.webkit) {
            browser.safari = true;
        }
    
        jQuery.browser = browser;
    }
    
    0 讨论(0)
  • 2020-12-08 11:01

    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');
     }
    
    0 讨论(0)
  • 2020-12-08 11:03

    I've noticed many different ways of doing this, this DOESN'T use $.support although I've found that this works very well. Also just as a note, jquery has just announced that they will be removing any support for IE 6,7,8 in jquery 2.0 (http://blog.jquery.com/2012/06/28/jquery-core-version-1-9-and-beyond/).

    var msVersion = navigator.userAgent.match(/MSIE ([0-9]{1,}[\.0-9]{0,})/),
          msie = !!msVersion,
          ie6 = msie && parseFloat(msVersion[1]) < 7;
    
      // Help prevent flashes of unstyled content
      if (!ie6) {
        //Do Something here.
      }
    

    Happy coding!

    0 讨论(0)
  • 2020-12-08 11:04

    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/

    0 讨论(0)
提交回复
热议问题