JavaScript
Article

jQuery Check Browser Version

By Sam Deering

Use jQuery to check your browser version in just a few lines of code you can optimise code for different browsers such as Firefox, IE, Safari, Chrome and more. The jQuery functions jQuery.Browser() and jQuery.Browser.Version() do all the hard work for us!

Here is how you do it.

//Example: output all version information about current browser

jQuery.each(jQuery.browser, function(i, val) {
  $("
" + i + " : " + val + "") .appendTo( document.body ); });

Example outputs:

  • Internet Explorer: 6.0, 7.0, 8.0
  • Mozilla/Firefox/Flock/Camino: 1.7.12, 1.8.1.3, 1.9
  • Opera: 10.06, 11.01
  • Safari/Webkit: 312.8, 418.9

 

//Example: Alerts “Do stuff for firefox 3” only for firefox 3 browsers.

var ua = $.browser;
if ( ua.mozilla && ua.version.slice(0,3) == "1.9" ) {
	alert( "Do stuff for firefox 3" );
}

//Example: Set a CSS property to specific browser.

if ( $.browser.msie ) {
	$("#div ul li").css( "display","inline" );
} else {
	$("#div ul li").css( "display","inline-table" );
}
Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.