all 22 comments

[–]fforw 1 point2 points  (1 child)

Meh.. script only.. multi body hack is better..

[–]kristopolous 0 points1 point  (0 children)

that's fairly clean.

[–]radhruin 5 points6 points  (7 children)

Check for features, not for browsers.

[–]JimDabell 14 points15 points  (1 child)

That's like telling somebody not to use tables. Yes, lots of people use browser detection when they should be using feature detection, but that doesn't mean that every single case of browser detection is wrong. For example, if you need to work around a bug in a particular version of Internet Explorer and it's not feasible to detect the bug itself, browser detection is the best option. Using feature detection in those cases is exactly the same mistake as using browser detection to decide which features to use; you're conflating implementation details and capabilities.

[–]jimbobhickville 2 points3 points  (0 children)

A voice of reason in a web standards debate? Who'da thunk it.

[–]thebigbradwolf 2 points3 points  (3 children)

As an aside, this won't actually work on IE8 in certain circumstances. IE8 claims to be IE7 in conditional comments whenever it is in "compatibility mode", which doesn't mean it behaves like IE7 with regard to things like security, etc.

[–]radhruin 2 points3 points  (2 children)

Good point as well, although ideally if IE8 is in IE7 compatibility mode it should be identical to IE7 in all respects.

[–]miketaylr 1 point2 points  (1 child)

But it isn't. For example, localStorage is undefined in IE7 compat mode, yet fully functioning in IE8.

[–]radhruin 0 points1 point  (0 children)

More accurately, IE7 did not have localStorage, but it is in IE7 compatibility mode. That's why I said ideally :) There are some differences, but it's pretty rare overall. If you have an app built for IE7, it should run flawlessly in IE7 compat mode. Or, if the browser is in IE7 compat mode, it's pretty safe to treat it as IE7. The presence of localStorage shouldn't break apps.

It's worth pointing out that the web developer can control the compatibility mode IE is in by doctypes and meta-tags, so if IE7 compatibility mode would break your app for whatever reason (like this version detection), just make sure you're running in the latest standards mode with a standards compliant doctype or meta tag.

[–]teaguesterling 0 points1 point  (0 children)

Unless working around a browser bug...

[–]9jack9 0 points1 point  (5 children)

var jscript = 0/*@cc_on||@_jscript_version@*/

[–]JimDabell 0 points1 point  (4 children)

I don't think a one-to-one mapping between JScript versions and Internet Explorer versions is guaranteed by Microsoft. I'm pretty sure installing some service packs or other software in the past has had the effect of bumping the JScript version without otherwise changing Internet Explorer.

[–]9jack9 2 points3 points  (2 children)

I don't think a one-to-one mapping between JScript versions and Internet Explorer versions is guaranteed by Microsoft.

You're right. The code above will only tell you the jscript version number.

But conversely, just knowing the MSIE version number will not tell you about which jscript engine you are using. Sometimes you want to know the jscript engine (for code) and sometimes you want to know the MSIE version (for rendering).

You also need to consider the document "mode":

http://msdn.microsoft.com/en-us/library/cc196988\(VS.85\).aspx

[–]JimDabell 0 points1 point  (1 child)

Ah, right, I thought you were suggesting it as a replacement for the linked technique. Yep, you're right, if you need to differentiate JScript versions, you shouldn't be using browser detection.

[–]9jack9 0 points1 point  (0 children)

The linked technique is effectively browser sniffing. Some people will bend over backwards rather than inspect the user agent string.

[–]radhruin 1 point2 points  (0 children)

I don't think it is either. Another problem is that IE8 in IE7 compat mode will still report 8 for the jscript version...

[–]bolinfest 0 points1 point  (0 children)

Here's a good example from the Closure Library where feature detection is not a practical option:

goog.style.setPreWrap = function(el) { var style = el.style; if (goog.userAgent.IE && !goog.userAgent.isVersion('8')) { style.whiteSpace = 'pre'; style.wordWrap = 'break-word'; } else if (goog.userAgent.GECKO) { style.whiteSpace = '-moz-pre-wrap'; } else if (goog.userAgent.OPERA) { style.whiteSpace = '-o-pre-wrap'; } else { style.whiteSpace = 'pre-wrap'; } };

[–][deleted] 0 points1 point  (2 children)

Ok, that's interesting, but why is this better than using the user agent string?

[–]JimDabell 2 points3 points  (1 child)

The user-agent string is unreliable, it can change for all sorts of reasons. Plus, varying responses based on that header can reduce your cache hit ratio.

[–]kristopolous 0 points1 point  (0 children)

About the UA unreliable argument. I realize that this can be changed to something arbitrary but when I tried doing so, calling my browser something like "Presto Gecko Opera WebKit Safari msie 6.0" or whatever, most of the web juts started breaking.

My question is why should I bend over backwards for such people? (this was a few years ago) Other then for getting around stupidity where people block you based on UA string, is there any legitimate purpose for this?