0

We know it's possible to look up the IP of a visitor using JSON and a callback, as seen here Can I perform a DNS lookup (hostname to IP address) using client-side Javascript? and also at http://news.ycombinator.com/item?id=1896015 - but what about a similar thing for the user agent?

Community
  • 1
  • 1
cwd
  • 53,018
  • 53
  • 161
  • 198

2 Answers2

5

This string is directly available in navigator.userAgent. However, this string is not very reliable, because the user can easily modify it. Only use it for non-critical purposes.

If you want to detect the user agent, so that you "know" what's supported, you'd better switch to capability-detection.

Examples of alternative methods to detect the browser:

  • IE: Conditional comments (HTML (also includes IE version) or in JScript)
  • Firefox / Webkit / Opera: Setting a CSS property with a vendor-prefix, and checking for the existence of the just-set property.
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • "this string is not very reliable" if it's client side JavaScript the user can directly edit your code so nothing is reliable. There's nothing especially unreliable about this property. – Andrew Downes May 10 '17 at 17:45
3

navigator.userAgent contains the user agent string of the browser.

cheeken
  • 33,663
  • 4
  • 35
  • 42