-1

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

Richard
  • 4,516
  • 11
  • 60
  • 87
  • Have you google it. This kind of thing is pretty easy to find. Maybe you can post your JS code here and we can figure out why it is not working in FF. – Aditya Sehgal Jul 11 '09 at 06:59
  • http://stackoverflow.com/questions/588940/what-is-the-best-way-to-do-browser-detection-in-javascript – Aditya Sehgal Jul 11 '09 at 07:00
  • @Aditya Sehgal - it's a damn fine question and even if it's easy to find on Google, it deserves it's place in stackoverflow.com – vsync Jan 15 '13 at 22:19
  • check this answer here, its THE one: http://stackoverflow.com/a/7690750/104380 – vsync Jan 15 '13 at 22:27

5 Answers5

2

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.

Artem Barger
  • 40,769
  • 9
  • 59
  • 81
1

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

rahul
  • 184,426
  • 49
  • 232
  • 263
0

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

Avinash
  • 49
  • 2
0
<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>
Vivian
  • 105
  • 6
0

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 
Amadu Bah
  • 2,919
  • 28
  • 27