Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a quick, pure javascript replacement for jquery.browser (removed in jquery 1.9)?

Yeah, I know that feature detection is preferable.

I've got a case in my codebase where we are using $.browser.msie and $.browser.version to decide whether to render some CSS or not. The developer who wrote the code is no longer with us, and I don't fully understand exactly what kind of feature detection I should write here instead.

As a quick fix, what's the shortest way to implement $.browser.msie and $.browser.version?

like image 942
ripper234 Avatar asked Jan 17 '13 12:01

ripper234


People also ask

What can I use instead of jQuery browser?

Nevertheless, Native javascript is one of the best jQuery alternatives, in fact, It is the framework of JS. Javascript is the best because any browsers ships with javascript and you do not need to install jQuery in your application.

How do you check which browser is being used in jQuery?

jQuery Detect Browser Furthermore, you can detect browser using jQuery library. You have to add the following jQuery library in HTML just before the <script src="script. js"></script> line.

Which method checks whether the specific features get supported by the browser in jQuery?

modernizr — Conditionally check to see if a specific feature is available in a browser.


1 Answers

I'll just copy the code from jQuery 1.8.3.

// Limit scope pollution from any deprecated API
(function() {

var matched, browser;

// Use of jQuery.browser is frowned upon.
// More details: http://api.jquery.com/jQuery.browser
// jQuery.uaMatch maintained for back-compat
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"
    };
};

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;
})();
like image 138
ripper234 Avatar answered Sep 24 '22 13:09

ripper234