I was trying to detect browser version and browser name using java script. I got so many techniques but I am happy with jquery’s one. It’s pretty simple and small block of code. Just check the bellow code that I got from jquery. As it is not possible to use the whole js library all the time but I like to use some part of it or follow the techniques for cross browser tasks. đ
[sourcecode language=’css’]
//Detect browser version
var userAgent = navigator.userAgent.toLowerCase();
// Figure out what browser is being used
var browser = {
version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [])[1],
safari: /webkit/.test( userAgent ),
opera: /opera/.test( userAgent ),
msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
};
alert(‘Browser Version=’+browser.version.toString()+ ‘ Safari=’+(browser.safari? ‘Yes’: ‘No’)+’ Opera=’+(browser.opera? ‘Yes’: ‘No’)+’ IE=’+(browser.msie? ‘Yes’: ‘No’)+’ FF=’+(browser.mozilla? ‘Yes’: ‘No’));
//end browser detection
[/sourcecode]
Edit: Here one thing you may be confused about the test() method. It’s a builtin function in js. The test() method is used to search for a match of a regular expression in a string.
Here’s some links about Test();
[code]i think this is also a simple method to detect browser
$(document).ready(function()
{
if ( $.browser.msie ){
if($.browser.version == â6.0â˛)
{
$(âhtmlâ).addClass(âie6â˛);
}
if($.browser.version == â7.0â˛)
{
$(âhtmlâ).addClass(âie7â˛);
}
if($.browser.version == â8.0â˛)
{
$(âhtmlâ).addClass(âie8â˛);
}
if($.browser.version == â9.0â˛)
{
$(âhtmlâ).addClass(âie9â˛);
}
}
else if ( $.browser.webkit )
{ $(âhtmlâ).addClass(âwebkitâ);
}
else if ( $.browser.mozilla )
{ $(âhtmlâ).addClass(âmozillaâ);
}
else if ( $.browser.opera )
{ $(âhtmlâ).addClass(âoperaâ);
}
});
[/code]
@Paulo Sossa
Thanks Paulo Sossa. but what do u mean by "any chance of pro version or extended paying support ?" .
awesome script exactly what i needed thank you so much…..such a time saver…any chance of pro version or extended paying support ?
Nice post this segment of code could reduce bulky browser detection code.
Thanks Manchu!