我可以从 JavaScript 中查找用户代理吗? [英] Can I look up the user agent from JavaScript?

查看:39
本文介绍了我可以从 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 属性,并检查刚刚设置的属性是否存在.

这篇关于我可以从 JavaScript 中查找用户代理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆