Question

My question is the folowing

Is there a solid javascript code to detect if a browser is IE(I don't care about others)

I found some script, but it generates an undefined error in FF.

I edited my question because I found some regex serverside solution for this on this forum It's enough for now.

thanks, Richard

Was it helpful?

Solution

You can find here very useful technique for IE detection using conditional tags.

<!DOCTYPE your favorite doc type> 
     <html> 
       <head>...</head>  
       <body>  
          <!--[if IE]>  
                  <div id="IEroot">  
          <![endif]-->  
                   <p id="IE">This browser is IE.</p>  
                   <p id="notIE">This browser is not IE.</p>  
          <!--[if IE]>  
                  </div>  
          <![endif]-->  
      </body> 
    </html> 

You can use it and put script tag between it to define Javascript action only for IE.

OTHER TIPS

Use the

navigator

object

navigator.appName

will give the browser name and

navigator.appVersion

will give the browser version.

Another one is listed here

Browser detect

You can check browser either by using Javascript or jQuery. Javascript provides Navigator object using that you can check like:

function getBrowserName()
  {
    if(navigator.appName=="Microsoft Internet Explorer") {
        alert("This is " + navigator.appName + " Browser");
  }
}

You can look example here: http://www.codegateway.com/2012/09/detect-ie-browser-javascript.html

And jQuery have Browser Object that we can use to detect browser like:

function getBrowser()
 {
   if ($.browser.msie) {
        alert('IE');
    }
 }

See example here: http://www.codegateway.com/2012/09/detect-ie-browser-jquery.html

<script type="text/javascript">
function getInternetExplorerVersion()

{
  var rv = -1; 
  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;
}
function checkVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    if ( ver >= 8.0 ) 
      msg = "You're using a recent copy of Internet Explorer."
    else
      msg = "You should upgrade your copy of Internet Explorer.";
  }
  alert( msg );
}
</script>

navigator.userAgent can be used. The following detect if browser is IE version <= 9:

var browserIEInfo = navigator.userAgent.match(/MSIE (([0-9]+)(\.[0-9]+)?)/);
if (browserIEInfo === null || parseInt(browserIEInfo[2]) > 9){
    // code for non IE or for IE version > 9
}

// code for IE <= 9 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top