我可以从 JavaScript 中查找用户代理吗?
我们知道可以使用 JSON 和回调来查找访问者的 IP,如下所示 我可以使用客户端 Javascript 执行 DNS 查找(主机名到 IP 地址)吗? 以及 http://news.ycombinator.com/item?id=1896015 - 但是对于用户代理来说类似的事情呢?
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?
这个字符串在 navigator.userAgent
.但是,这个字符串不是很可靠,因为用户可以很容易地修改它.仅将其用于非关键目的.
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:条件注释(HTML(也包括 IE 版本)或 在 JScript 中)
- Firefox/Webkit/Opera:使用供应商前缀设置 CSS 属性,并检查刚刚设置的属性是否存在.