jQuery 1.8 – $.browser has been deprecated

With the new jQuery release 1.8 they are going to remove the $.browser method (definitely removed on the 1.9 v.):

 $.browser: Ever since jQuery 1.4, we’ve been evangelizing that browser detection via the user agent string is a bad idea. Yet we’ve been an enabler of bad practice by continuing to offer $.browser. As of jQuery 1.9 we’ll remove it entirely and you’ll need to use the 1.9 compat plugin. If your code isn’t weaned off browser detection yet, check out Modernizr for a very thorough set of feature detections you can use instead. And of course, you’re welcome to read the tea leaves in the navigator.userAgent string directly, there’s nothing stopping you but your conscience.

As many of my components are using this method for browser detection I’ll make a global refactor for all of them. Anyway I found out that $.browser.webkit doesn’t work anymore for Chrome with jQuery 1.8.

You can fix it either pointing to the previous version of jQuery (1.7) or adding this code before the components javascript files are loaded:

/*Browser detection patch*/
jQuery.browser = {};
jQuery.browser.mozilla = /mozilla/.test(navigator.userAgent.toLowerCase()) && !/webkit/.test(navigator.userAgent.toLowerCase());
jQuery.browser.webkit = /webkit/.test(navigator.userAgent.toLowerCase());
jQuery.browser.opera = /opera/.test(navigator.userAgent.toLowerCase());
jQuery.browser.msie = /msie/.test(navigator.userAgent.toLowerCase());

This catches the browser vendor but not the browser version, so $.browser.version will fire an exception.

I wrote an extended plugin that replaces the $.browser object including many information retrieved from the user agent;

you can get it from jsfiddle: http://jsfiddle.net/pupunzi/dnJNS/

or download it here: https://github.com/pupunzi/jquery.mb.browser/archive/master.zip

I know this is not very clean and I’m looking for something better, but it’s the faster way to solve it :-).