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?
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.
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.
modernizr — Conditionally check to see if a specific feature is available in a browser.
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;
})();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With