我可以从 JavaScript 查找用户代理吗?

发布于 2024-12-28 20:44:54 字数 349 浏览 3 评论 0原文

我们知道可以使用 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀟灑尐姊 2025-01-04 20:44:54

该字符串可直接在 navigator.userAgent。然而,这个字符串不是很可靠,因为用户可以很容易地修改它。仅将其用于非关键目的。

如果您想检测用户代理,以便您“知道”支持什么,您最好切换到功能检测。

检测浏览器的替代方法示例:

  • IE:条件注释(HTML(还包括 IE 版本)或 中JScript)
  • Firefox / Webkit / Opera:使用供应商前缀设置 CSS 属性,并检查刚刚设置的属性是否存在。

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.
扎心 2025-01-04 20:44:54

navigator.userAgent 包含浏览器的用户代理字符串。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文