views:

292

answers:

3

I have a spec in my current project that requires us to advise the user which browsers are best to use the web application. If their current browser version they are using is not in our list of "ideal" browsers we want to display a message.

What is the best way to check a specific version of the users browser. I am aware of the following using jQuery but this doesn't help with specific versions.

$(document).ready(function() {
   var b = '';
   $.each($.browser, function(i, val) {
       if (i=='safari' && val==true) { b = 'safari'; }
       if (i=='opera' && val==true) { b = 'opera'; }
       if (i=='msie' && val==true) { b = 'msie'; }
       if (i=='mozilla' && val==true) {b = 'mozilla'; }
   });

   //Do Something With b, Like $('#dis').html(b);
});

We want to be able to say is your browser Firexfox 2 or greater or IE6 or greater etc?

+2  A: 

Check out the YUI User-Agent Detection.

EDIT: Now that I've told you how, I just want to make sure you know that this is generally considered an antipattern, right? If you can, I'd recommend not doing something like this, but I realize that's not always an option.

Hank Gay
Thanks for that but I'm not sure how the YUI detection differers from the JQuery example I gave? It still doesn't get the browser version eg Firefox 3.0.5. It only returns Firefox. It would also be best if we could use the JQuery library of standard JavaScript as that is what we are already using.
Sheff
I'm pretty sure it returns a version number, not a simple boolean.
Hank Gay
+4  A: 

Also check for $.browser.version in the docs.jquery.com

It can return 2.0 for Firefox 2.x.x, check the docs :)

Ricardo Vega
+3  A: 

Here is a JQuery plugin that'll help

SquidScareMe