2

I am interested if which would be the best place to detect the client's user-agent, client-side (javascript) or server-side? I brought up the question due to the fact that some IE8 users are getting a message saying they're using IE6.

1
  • Tangentially related note: The user agent says MSIE 7.0 instead of MSIE 8.0 when using compatibility view in IE8.
    – Brian
    Jun 3, 2010 at 18:40

5 Answers 5

8

The short and correct answer is : do not use anything that relies on UserAgent sniffing.

To reliable be able to adjust code paths you should test for the specific 'thing' that the codepath is adjusted for, primarily features. This is called Feature Detection.

So if feature X is supported we do this, if not we do that.

Deducing if a feature is supported based on which UserAgent is present will rapidly fail, especially when new browsers come to the marked.
Take the following example, which can actually be found in several major libraries (!)

if (isIE8) {
    // use new feature provided by IE8
} else if (isIE7) {
    // use not so new feature provided by IE7 (and IE8)
} else {
    // use fallback for all others (which also works in IE7 and IE8)
}

What do you think happens when IE9 comes along?

The correct pattern in this case would be

if ("addEventListener" in foo) {
    // use DOM level 2 addEventListener to attach events
    foo.addEventListener(...
} else if ("attachEvent" in foo) {
    // use IE's proprietary attachEvent method
    foo.attachEvent(...
} else {
    // fall back to DOM 0
    foo["on" + eventName] = ....
}
5
  • 2
    em.. remove it? Code should use Feature Detection and not UserAgent Sniffing to adapt its codepaths Jun 3, 2010 at 19:04
  • +1. Sean is absolutely correct. Peter Michaux's article on feature detection is a good place to start: michaux.ca/articles/…
    – Tim Down
    Jun 3, 2010 at 19:13
  • 1
    -1: Sean, this is a really bad answer. I can see that the comments mentions feature detection, but you don't even mention anything, you just say what not to use, which doesn't really help anyone coming to this question in the future.
    – andy
    Jun 4, 2010 at 3:58
  • @Andy, you are quite right, normally I would have pointed to the same article mentioned by @Tim. I'll update the answer with this. Jun 4, 2010 at 12:53
  • @Sean, Okay, I agree with this answer, but then how do you handle coding a javascript video player that needs to treat iPad and iPhone differently because of their html5 video autoplay non-functionality? You can't check for the feature because they support it, but it just doesn't work by design.
    – Corgalore
    Feb 9, 2012 at 15:23
5

The User-agent available on both sides should be the same, unless there's funny stuff going on, which normally isn't.

If you want to show a message to IE6 users, I suggest you use conditional comments. They're an IE-specific feature and work very well for detecting IE versions.

0
0

The information found through client or server-side detection is basically the same.

Keep in mind it is extremely easy to spoof what browser you're in. There is no fail-safe way to detect all browser types accurately.

0

i don't know how you're checking for the user agent, but i'd do this way:

<%=
case request.env['HTTP_USER_AGENT']
when /Safari/
    "it's a Mac!"
when /iPhone/
    "it's a iPhone"
else
    "i don't know :("
end
%>

checking directly in the user request seems to be the most consistent way to verify the user browser. And the request.env is avaliable in your controller and views, so you could pass this to rjs if needed.

0

For those who need to get the actual user-agent using JavaScript, you can use navigator.userAgent

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.