browser.msie error after update to jQuery 1.9.1

前端 未结 11 1865
别那么骄傲
别那么骄傲 2020-11-27 03:32

I use the following snip of a script:

if ($.browser.msie && $.browser.version < 9) {
   extra = \"?\" + Math.floor(Math.random() * 3000);
}


        
相关标签:
11条回答
  • 2020-11-27 03:45

    Instead of having the whole migration script added, you could simply add the following (extracted from the migration script)

    $.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"
        };
    };
    

    and then use it like so

    $.uaMatch(navigator.userAgent)
    
    0 讨论(0)
  • 2020-11-27 03:47

    Since $.browser is deprecated, here is an alternative solution:

    /**
     * Returns the version of Internet Explorer or a -1
     * (indicating the use of another browser).
     */
    function getInternetExplorerVersion()
    {
        var rv = -1; // Return value assumes failure.
    
        if (navigator.appName == 'Microsoft Internet Explorer')
        {
            var ua = navigator.userAgent;
            var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
            if (re.exec(ua) != null)
                rv = parseFloat( RegExp.$1 );
        }
    
        return rv;
    }
    
    function checkVersion()
    {
        var msg = "You're not using Internet Explorer.";
        var ver = getInternetExplorerVersion();
    
        if ( ver > -1 )
        {
            if ( ver >= 8.0 ) 
                msg = "You're using a recent copy of Internet Explorer."
            else
                msg = "You should upgrade your copy of Internet Explorer.";
        }
    
        alert( msg );
    }
    

    Source

    However, the reason that its deprecated is because jQuery wants you to use feature detection instead.

    An example:

    $("p").html("This frame uses the W3C box model: <span>" +
            jQuery.support.boxModel + "</span>");
    

    And last but not least, the most reliable way to check IE versions:

    // ----------------------------------------------------------
    // A short snippet for detecting versions of IE in JavaScript
    // without resorting to user-agent sniffing
    // ----------------------------------------------------------
    // If you're not in IE (or IE version is less than 5) then:
    //     ie === undefined
    // If you're in IE (>=5) then you can determine which version:
    //     ie === 7; // IE7
    // Thus, to detect IE:
    //     if (ie) {}
    // And to detect the version:
    //     ie === 6 // IE6
    //     ie > 7 // IE8, IE9 ...
    //     ie < 9 // Anything less than IE9
    // ----------------------------------------------------------
    
    // UPDATE: Now using Live NodeList idea from @jdalton
    
    var ie = (function(){
    
        var undef,
            v = 3,
            div = document.createElement('div'),
            all = div.getElementsByTagName('i');
    
        while (
            div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
            all[0]
        );
    
        return v > 4 ? v : undef;
    
    }());
    
    0 讨论(0)
  • 2020-11-27 03:52

    Include jQuery migration plugin along with your jQuery library.

    0 讨论(0)
  • 2020-11-27 03:52

    You can detect the IE browser by this way.

    (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1)

    you can get reference on this URL: jquery.browser.msie Alternative

    0 讨论(0)
  • 2020-11-27 03:55

    Using this:

    if (navigator.userAgent.match("MSIE")) {}

    0 讨论(0)
  • 2020-11-27 03:58

    You can use :

    var MSIE = jQuery.support.leadingWhitespace; // This property is not supported by ie 6-8
    
    $(document).ready(function(){
    
    if (MSIE){
        if (navigator.vendor == 'Apple Computer, Inc.'){
            // some code for this navigator
        } else {
           // some code for others browsers
        }
    
    } else {
        // default code
    
    }});
    
    0 讨论(0)
提交回复
热议问题