GreatWebGuy

Self-proclaimed greatness is a hard thing to prove

Browser Detection Shim for jQuery 1.9

We started the process of getting rid of deprecated methods in order to migrate to jQuery 1.9, but the removal of $.browser was a no go for us. I pulled jQuery’s browser detection code and packaged it into a closure, with a few minor changes for past encountered bugs, and then simply replaced all instances of “$.browser” in my scripts with “browser”. Add this script above the rest of your scrips and make the replace and you’ll have your browser detection back. Things like bgiframe() are still necessary for some of us supporting B2B clients on antique browsers, it just isn’t possible to use feature detection for some of these cases. I noticed in the comments section on jquery.com for the release of 1.9.1 some people asking for this, hope this helps.

//Browser detection//
var browser = (function (pub) {
	var matched, browserObj;
	uaMatch = function(ua) {
		ua = ua.toLowerCase();
		//This fixes an ie7 bug that causes crashes from incorrect version identification
		if(/*@cc_on/*@if(@_jscript_version<=5.6)1@else@*/0/*@end@*/) {
			ua = "msie 6.0";
		}
		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 {
			browserObj: match[ 1 ] || "",
			version: match[ 2 ] || "0"
		};
	};

	matched = uaMatch(navigator.userAgent);
	browserObj = {};

	if ( matched.browserObj ) {
		browserObj[ matched.browserObj ] = true;
		browserObj.version = matched.version;
	}
	// Chrome is Webkit, but Webkit is also Safari.
	if (browserObj.chrome) {
		browserObj.webkit = true;
	} else if (browserObj.webkit) {
		browserObj.safari = true;
	}

	pub = browserObj;	
	return pub;
}(browser || {}));

Comments