July 27, 2008 | Posted by Manchumahara(Sabuj Kundu) in category Java script with tags , , , , ,

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. :D

//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

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();

  1. Email Address valiadtion
  2. W3 school link
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

3 Responses to Detect browser name and version using js

  1. Nice post this segment of code could reduce bulky browser detection code.

    Thanks Manchu!

  2. Paulo Sossa says:

    awesome script exactly what i needed thank you so much…..such a time saver…any chance of pro version or extended paying support ?

  3. admin says:

    @Paulo Sossa

    Thanks Paulo Sossa. but what do u mean by “any chance of pro version or extended paying support ?” .

Leave a Reply

Additional comments powered by BackType

feedback