Azer Koçulu
May 11th, 2010

Detecting IE

I've seen a detecting code when I was looking over developer pages of IE8:

function getInternetExplorerVersion()
 var rv = -1; // Return value assumes failure
 if (navigator.appName == 'Microsoft Internet Explorer')
  var ua = navigator.userAgent;
  var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  if (re.exec(ua) != null)
   rv = parseFloat( RegExp.$1 );
 return rv;

I think that there are lots of better ways to detecting browser version.Here is my one-line solution;

var IEVersion = /msie/i.test(navigator.userAgent) &&  parseInt(navigator.userAgent.match(/ie (\d)/i)[1]) || -1

We can use it to get Firefox version with some modifying;

/firefox/i.test(navigator.userAgent) && parseInt(navigator.userAgent.match(/firefox\/(\d)/i)[1]) || -1

As you see, test method of regular expressions is really useful, I had seen this technique at Hakan Bilgin's awesome extend.js library firstly.