views:

101

answers:

5

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?

A: 

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
A: 

Try this:

alert(navigator.appName);
Darin Dimitrov
I like this answer's simplicity. Does anyone have objections to it, because though the other answers are more comprehensive (cover version number, etc.) all I need is the browser type. But any known problems here. By the way, I tried it on FF and it said Netscape!
Duber
A: 

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
+3  A: 

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

Jonas Elfström
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
A: 

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.
WmasterJ
This answer will help you regardless of a javascript framework, which I believe is the best starting point.
WmasterJ