Can I look up the user agent from JavaScript?

非 Y 不嫁゛ 提交于 2019-12-12 03:05:27

问题


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?


回答1:


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.



回答2:


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



来源:https://stackoverflow.com/questions/9009774/can-i-look-up-the-user-agent-from-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!