10

I'm trying to use this line to detect browser type: IE or Firefox.

alert(isBrowser("Microsoft"));

but I get absolutely nothing, the alert doesn't even pop up. Not sure what I'm doing wrong.

What would be the best practice way to detect browser type?

Duber
  • 125
  • 1
  • 1
  • 5

8 Answers8

9

I hope this helps:

http://www.quirksmode.org/js/detect.html

(it's a long script, so i don't want to post it here)

lajuette
  • 937
  • 1
  • 5
  • 17
8

Try this:

alert(navigator.appName);
Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
4

I think jQuery got it right when they support testing for features instead of just browser.

Jonas Elfström
  • 28,718
  • 6
  • 66
  • 102
  • 1
    I agree with the theory - and mostly it works in practice too. But occasionally you need to know the browser. For example how do you detect right-mouse-click in webkit (safari or chrome) even prototype.js has to resort to browser detection here – plodder Mar 30 '10 at 23:25
3

For MSIE detection you may use JavaScript:

   // This function returns Internet Explorer's major version number,
   // or 0 for others. It works by finding the "MSIE " string and
   // extracting the version number following the space, up to the decimal
   // point, ignoring the minor version number
   <SCRIPT LANGUAGE="JavaSCRIPT">
   function msieversion()
   {
      var ua = window.navigator.userAgent
      var msie = ua.indexOf ( "MSIE " )

      if ( msie > 0 )      // If Internet Explorer, return version number
         return parseInt (ua.substring (msie+5, ua.indexOf (".", msie )))
      else                 // If another browser, return 0
         return 0

   }
   </SCRIPT>

Below is an example of how to call it anywhere in your html:

<SCRIPT LANGUAGE="javascript">
   if ( msieversion() >= 0 )

      document.write ( "This is Internet Explorer" );

   else

      document.write ( "This is another browser" );

   </SCRIPT>

http://support.microsoft.com/kb/167820 http://support.microsoft.com/kb/167820

1

A very good article on this comes from Quirksmode: http://www.quirksmode.org/js/support.html

The script supplied by 'lajuette' is good but it doesn't make you much smarter. The same author explain his thinking behind the script in the above link and basically what he says is:

  • It is not about browser detection
  • It is about object detection
  • This leads to the knowledge of which browser is used.
JeroenEijkhof
  • 2,122
  • 2
  • 24
  • 37
0

This is basic for browser type detection but from this littel code its difficult to understand what going wrong.... Can u add body of isBrowser() that will help.

Anil Namde
  • 5,764
  • 9
  • 58
  • 96
-1

The best and shortest way to find the browser type for IE is.. U can do same for other browser types

if (navigator.appName == "Microsoft Internet Explorer"){

// Ur piece of validation 

}
Rob
  • 4,809
  • 12
  • 48
  • 49
Nitesh
  • 9
  • it will work only for IE 10 and earlier versions. Check it [here.](http://www.w3schools.com/jsref/prop_nav_appname.asp) – Alex.K. Oct 14 '14 at 09:17
-1
function whereUWantToDetectBrowser(){
        if (navigator.appName == "Microsoft Internet Explorer")
        intExp();
        else
        other();

}
function intExp(){
        //do what you want to do for specifically Internet Explorer
}
function other(){
        //do what you want to do for other browsers
}

this code solves the problem. Instead of calling functions from whereUWantToDetectBrowser(), if you write your specific code there, this will cause an error. And code will not run. Because a browser detects the code which it has to run (specific to each browser). and if you are distinguishing the code means the code is not working in some browsers, so you want to write it specifically for those browsers. So, other() will have not effect in IE, as intExp() has no effect in other browsers.

Flexo
  • 82,006
  • 22
  • 174
  • 256
eagle
  • 396
  • 1
  • 5
  • 18