1

I've found an article that explains how to detect IE "by checking existence of nonstandard document.all object available in IE only".

Would this method be considered good practice? Is it stable and really future proof?

Here is the article: http://tanalin.com/en/articles/ie-version-js/

Also, don't forgot the section of future proofing: http://tanalin.com/en/articles/ie-version-js/#h-future-proof

Here is the code suggested:

if (document.all && !document.querySelector) { // IE7 or lower

}
shrewdbeans
  • 11,971
  • 23
  • 69
  • 115
  • 2
    Nothing is "really future proof". – Ted Hopp Oct 16 '12 at 15:36
  • 1
    You can use conditional comments to detect IE versions. [Javascript IE detection, why not use simple conditional comments?](http://stackoverflow.com/questions/4169160/javascript-ie-detection-why-not-use-simple-conditional-comments) This technique can't be spoofed with user agent manipulations. – I Hate Lazy Oct 16 '12 at 15:36

4 Answers4

4

Would this method be considered good practice?

No

Is it stable and really future proof?

No

In general, browser detection is not good practice.

In those occasions when it is the best solution to a problem, and the browsers you need to detect are versions of IE, then Conditional Comments are the way to go.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 2
    +1 for "browser detection is not good practice". Much better to use [feature detection](http://stackoverflow.com/questions/1294586/browser-detection-versus-feature-detection). – Ted Hopp Oct 16 '12 at 15:37
2

I'm with @Quentin, especially if you're not really "at home" in JS. I don't mean anything bad with that, but in rare cases, you can use a full browser detect script, but unless you can fully understand the code, I suggest you don't use it.

There's a good article on quirksmode that does a good job at explaining why.
Also on quirksmode, you can find a full browser detection script. But as I said: unless you can honestly say you fully comprehend the code, I'd suggest you leave it at that for now...

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149
1

Browser detection is a fallacy.

What you should focus on is 'feature detection', which is a form of duck-typing.

Put another way, you're less interested about whether a user is running a specific browser version and more interested in whether the browser they are running (regardless of brand or version) supports the features that you would like to use.

The advantage of doing this is that there are many ways to add features, beyond upgrading a browser. Libraries like Modernizr add 'missing' features via JavaScript implementation. Browser detection would miss this.

The 'advantage' of browser detection is that it's a shorthand for lots of individual feature detection code. However, this can be a false economy.

In general, do one of the following:

  1. If you're writing a library, be explicit about every feature you wish to test for.
  2. If you're not writing a library, use a library.
Dancrumb
  • 26,597
  • 10
  • 74
  • 130
  • I agree with _everything_ you say, except for one thing: _if you're not writing a library, use a library_. I'd say: don't use a library regardless, know the pro's & con's. Often ppl use libraries, thinking it's faster and more reliable when really, there was no need to include a vast lump of code. Writing 2 additional lines _can_ be enough – Elias Van Ootegem Oct 16 '12 at 15:58
  • @EliasVanOotegem Yeah, I take you're point. However, I'd say that unless you *know* that a library is unnecessary, you're probably better off using one, rather than burning time and effort debugging all of the corner cases that a library already covers. – Dancrumb Oct 16 '12 at 16:37
  • 1
    Absolutely! No need to reinvent the wheel, let alone on a daily basis :)... I can just get annoyed by jQuery being regarded as a _superset_ of JavaScript. God knows how many questions you see here everyday going _"How do you write this in JavaScript in jQuery"_ or _The solution can be written in either JavaScript or jQuery_. I couldn't resist suggesting that sometimes, perhaps, people should take the time to look at what JS is really all about. They might end up with a better understanding of the libs they've always used, and they might use them more efficiently. – Elias Van Ootegem Oct 16 '12 at 16:42
0

It's always better to detect new features directly. The article is solely about detecting old (older than current stable one) versions of IE in cases when detecting a feature/bug directly is impossible or would be overkill considering that all browsers except old IEs do support the feature long ago.

Marat Tanalin
  • 13,927
  • 1
  • 36
  • 52