-1

I have heard that browser sniffing is bad , i have seen the below method used to detect if a browser is IE or not:

var isIe = !!window.ActiveXObject,

is it a reliable way to detect ie ?

I have seen this method used in plugins like HERE

Alexander Solonik
  • 9,838
  • 18
  • 76
  • 174
  • I would go for `navigator.userAgent.indexOf('Trident') >= 0` to detect IE and `navigator.userAgent.indexOf('Edge') >= 0` for Edge, this one seems... Ungainly. I'd rather not see the words `ActiveXObject` on my screen ever again :) (And yes, I am aware of sniffing user agents, but for most use cases this is fine) – somethinghere Sep 02 '15 at 15:21
  • 2
    You're missing the point. Browser sniffing isn't bad because the **methods** of browser sniffing are bad. The entire practice is bad, regardless of how you do it. Your method happens to be particularly bad though. Why would you ever need to know whether the browser is *any version* of IE? You're lumping IE11 in with IE6. That can't possibly be useful. You should be attempting to detect individual features prior to their use. – user229044 Sep 02 '15 at 15:21
  • 1
    What usually makes browser sniffing bad is what is done with the information. The first choice is still feature detection, not browser detection. – jfriend00 Sep 02 '15 at 15:21
  • @jfriend00 that makes sense ! and yes i have messed with modernizer a bit , but i found this new method rather quite interesting . – Alexander Solonik Sep 02 '15 at 15:23
  • In answer to your question, "Still Bad". – jfriend00 Sep 02 '15 at 15:23
  • @meagar , you might be right , but people are using it , have a look https://github.com/yairEO/fancyInput/blob/master/fancyInput.js#L10 – Alexander Solonik Sep 02 '15 at 15:24
  • @jfriend00 taught so .... but had to ask :) – Alexander Solonik Sep 02 '15 at 15:25
  • Just because people are using it does not mean it is appropriate to use it for all possible purposes nor does it even mean that they are using it for an appropriate purpose. The fact that others are using it only means that some people find it useful for something. That doesn't mean it works for your particular use or that it is the most appropriate solution for your particular use. – jfriend00 Sep 02 '15 at 16:32

1 Answers1

1

Except in very rare cases (usually dealing with implementation bugs for which feature detection is not practical), making programming decisions based on the brand or version of the browser is considered a poor practice that leads to code that breaks as browsers change.

Creating a variable like isIE and then using that for programming decisions makes an assumption that all browsers for which isIE would be true or false all have the same behavior and this is exactly where browser detection breaks down. It's just wrong.

For example, IE8 didn't support .addEventListener(), but IE9 and beyond does. And then in IE11, .attachEvent() was removed.

So, if awhile ago, you used browser detection and wrote code like this:

if (isIE) {
    elem.attachEvent(...);
} else {
    elem.addEventListener(...);
}

Then, when IE started supporting .addEventListener() in IE9 your code would continue to use the old way. Then, when IE11 came out when .attachEvent() was removed, your code would break entirely because you'd still be detecting IE and trying to use the old way. So, your code was brittle because it assumes that detecting a browser type means a specific behavior which is just simply not true. Instead, you should detect the behavior itself, not the type of browser.

So, using feature detection:

if (elem.addEventListener) {
    elem.addEventListener(...);
} else {
    elem.attachEvent(...);
}

Then, your code just merrily continues to work in IE8, IE9, IE10, IE11, etc... Your code doesn't have to be "fixed" as the browser changes.

jfriend00
  • 683,504
  • 96
  • 985
  • 979