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?
4 Answers 4
Here is a JQuery plugin that'll help
Comments
Also check for $.browser.version in the docs.jquery.com
It can return 2.0 for Firefox 2.x.x, check the docs :)
Comments
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.
2 Comments
Internet Explorer 10 and above versions behave differently from IE 9 and below. When using javascript you need to handle those scenarios differently. Following code worked for me :)
//MSStream object supported only for IE 10 and 11 (hope this will work for above IE 11 too .. )
var isIE10or11 = window.MSStream;
//FormData object allow you to send form data as key and value pairs with ajax requests. Supported in modern browsers.
var isFormDataSupported = (window.FormData !== undefined);
if(isIE10or11 && isFormDataSupported){
alert('IE 10 or 11');
}
else if(!isIE10or11 && isFormDataSupported){
alert('HTML 5 browser Excluding IE');
}
else{
//Neither supports MSStream nor FormData object
alert('IE Version 9 or below');
}