Monday, July 23, 2012

How to really get the right version of Internet Explorer in JavaScript



This is a simple function to really return the right version of Internet Explorer. Pieces are cobbled together from my old work and dojo's inner libraries too.

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  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 );
//In cases where the page has an HTTP header or META tag with
//X-UA-Compatible, then it is in emulation mode.
//Make sure isIE reflects the desired version.
//document.documentMode of 5 means quirks mode.
//Only switch the value if documentMode's major version
//is different from isIE's major version.
var mode = document.documentMode;
if(mode && mode != 5 && Math.floor(rv) != mode){
rv = mode;
}
  }
  return rv;
}

No comments:

Post a Comment