shrewdbeans
shrewdbeans

Reputation: 12549

Is this a clean and good way to detect versions of IE?

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

}

Upvotes: 1

Views: 156

Answers (4)

Marat Tanalin
Marat Tanalin

Reputation: 14123

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.

Upvotes: 0

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76433

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...

Upvotes: 2

Dancrumb
Dancrumb

Reputation: 27579

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.

Upvotes: 1

Quentin
Quentin

Reputation: 944210

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.

Upvotes: 4

Related Questions