JavaScript
Article
By Sam Deering

jQuery Check Browser Version

By Sam Deering
Help us help you! You'll get a... FREE 6-Month Subscription to SitePoint Premium Plus you'll go in the draw to WIN a new Macbook SitePoint 2017 Survey Yes, let's Do this It only takes 5 min

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

--ADVERTISEMENT--

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" );
}
Login or Create Account to Comment
Login Create Account
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.Is it good?