The Simplest Effective Browser Sniffer
ALL OF THE FOLLOWING JAVASCRIPT ABOUT BROWSER DETECTION SEEMS TO WORK ON JUST ABOUT ALL PLATFORMS AND JUST ABOUT ALL BROWSERS, AS LONG AS JAVASCRIPT IS TURNED ON, unless you are using IE11. See below if you have IE11. Don't use the "Except for IE11" code! Use the "Good on everything" code lower on the page.
Except for IE11
Note: We chose to only test for Safari and IE on the Mac, so figure it's Mac Firefox if it isn't Mac IE or Mac Safari but it is Mac. The Netscape/Firefox/Mozilla test is all one variable: Netscape. There's no use testing for Netscape, Mozilla, or Firefox on the Mac since Safari tests erroneously indicate true for the Netscape tests—why Safari wants to be seen as Netscape is anyone's guess, and my guess is that with primitive sniffers that see only IE and Netscape/Firefox/Mozilla, the non-IE Mac browsers are a lot more likely to display well with Netscape-browser-targeted code than IE code. (This would be Apple's way of dealing with the "who cares about little old Mac?" webmasters.) We leave out Konqueror or other minor browsers since they need to either have the good sense to be detected as one of the main browser types like IE or Netscape/Firefox/Mozilla or get out of the browser game. It's true that various tricks can be played to create false browser sniffing results, so these tests are not infallible, but the trouble with object detection is that the browser may begin supporting this object, which makes such tests go awry. However, window.opera and document.all seem like safe bets for object detection for a long time to come. (Or so we thought—document.all is gone for IE11.) Chrome thinks it's Netscape and Windows Safari as well as Chrome, but it isn't—it's only Chrome.
Except for IE11
mactest=(navigator.userAgent.indexOf("Mac")!=-1) //My browser sniffers
is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1
Netscape=(navigator.appName.indexOf("Netscape") != -1)
msafari=(navigator.userAgent.indexOf("Safari")!= -1)
wsafari=0; if(!mactest&&msafari){wsafari=1;msafari=0}
is_opera = 0; if(window.opera){is_opera=1}
is_ie_mac = 0; is_ie=0;if(document.all){is_ie=1}
if(is_ie&&mactest){is_ie_mac=1}
alert("mactest:"+mactest+", Netscape:"+Netscape+", wsafari:"+wsafari+", msafari:"+msafari+", is_ie:"+is_ie+", is_ie_mac:"+is_ie_mac+", is_opera:"+is_opera+", is_chrome:"+is_chrome)
Good on everything
Note: document.all has remained in Internet Explorer through version 10. As of 11, it is gone! So is_ie=1 won't happen but is_ie=0 will. And MSIE is gone from the userAgent in 11. We still use it to find older versions of IE pre-11. But for IE11 we need to be looking for Trident/ in the appVersion (this works for 9 and 10 as well as 11). And navigator.appName is now set to “Netscape” so the Netscape=(navigator.appName.indexOf("Netscape") != -1) test will determine that IE11 is Gecko—like Firefox! JavaScript-based logic for browser detection that uses the Netscape=(navigator.appName.indexOf("Netscape") != -1) test will end up identifying Internet Explorer 11 as a Gecko-based browser. This will screw up DHTML that uses this type of test for browser detection and then (usually) display manipulations such as changing top or left values of Divs using the getElementById method. Note the Netscape=(navigator.appName.indexOf("Netscape") != -1 && is_ie != 1) code that lets Netscape be a true variable value only if the browser found is NOT any IE. Our detector will give Netscape a false value if you're using any IE even though IE11 tries to fool you and say it is true.
One point in Microsoft's favor regarding Internet Explorer 11 is that it is standards compliant, so when it is detected as a Gecko browser and the navigator.appName is seen as Netscape, it will act like Firefox so much so that you don't usually need extra dynamic display DHTML. We have found exceptions, though, and having the detector below will be important at such times.
Good on everything
mactest=(navigator.userAgent.indexOf("Mac")!=-1) //My browser sniffers
is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1
msafari=(navigator.userAgent.indexOf("Safari")!= -1)
wsafari=0; if(!mactest&&msafari){wsafari=1;msafari=0}
is_opera = 0; if(window.opera){is_opera=1}
is_ie_mac = 0;
is_ie=0;if(navigator.userAgent.indexOf('MSIE') !==-1 || navigator.appVersion.indexOf('Trident/')>0) {is_ie=1};
Netscape=(navigator.appName.indexOf("Netscape") != -1 && is_ie != 1)
if(is_ie&&mactest){is_ie_mac=1}
alert("mactest:"+mactest+", Netscape:"+Netscape+", wsafari:"+wsafari+", msafari:"+msafari+", is_ie:"+is_ie+", is_ie_mac:"+is_ie_mac+", is_opera:"+is_opera+", is_chrome:"+is_chrome)