0

I wish to detect various browsers with feature detection in JavaScript from IE7-11, Chrome and Firefox. How can I do this>

Johnathan Au
  • 5,244
  • 18
  • 70
  • 128
  • 1
    Then it's no longer feature detection, but browser detection, the thing one tries to avoid by using feature detection ? – adeneo Apr 14 '15 at 17:39
  • 1
    The whole point of feature detection is to not do that. – SLaks Apr 14 '15 at 17:39
  • 1
    you can look for "smoking guns" associated with each browser you're looking for. not every single version of all browsers has one of those, but each version of IE will for sure. work your way backwards on caniuse to find globals supported by certain IE version, ex btoa, XMLHttpRequest, FileReader(), etc. – dandavis Apr 14 '15 at 17:42
  • Ok, I clearly got it all mixed up. Can someone point me in the right direction? :) – Johnathan Au Apr 14 '15 at 17:45
  • The way that feature detection should be used is that when you need to do something, you check if that feature is available, and then use it. If it's not, then you (hopefully) fall back to some alternative method. If that alternative method isn't universally supported, then you'd need to check if whatever feature it uses is supported, and then use it. if not, then fall back again. Rinse and repeat as necessary. Doing it like this, you never need to care about which browser is being used, only which features are supported. – Elezar Apr 14 '15 at 18:03
  • possible duplicate of [Improving a Javascript browser detection function](http://stackoverflow.com/questions/13316353/improving-a-javascript-browser-detection-function) – A. Magalhães Apr 14 '15 at 19:04
  • http://arasatasaygin.github.io/is.js/ – user3818383 Apr 14 '15 at 19:06
  • Checkout http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser#answer-9851769 – kiranjith Apr 16 '15 at 21:03

1 Answers1

0

As @dandavis said, look objects specific to the browser you need. For example:

if (!document.characterSet || !document.doctype || !document.defaultView) {
        /**
         * If the browser does not have these properties on the document,
         * then it is probably IE8 or lower... Maybe.
         */
}
colecmc
  • 3,133
  • 2
  • 20
  • 33