Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

NavigatorID.userAgent

概述

返回当前浏览器的用户代理(user agent)字符串。

语法

var ua = window.navigator.userAgent;

该属性是只读的.

用户代理字符串是由其他的一些 navigator 对象的属性值组合而成的。基于 Gecko 的浏览器中,其结构如下:

userAgent = appCodeName/appVersion number (Platform; Security; OS-or-CPU; 
Localization; rv: revision-version-number) product/productSub 
Application-Name Application-Name-version

例子

alert(window.navigator.userAgent)
// 弹出 "Mozilla/5.0 (Windows; U; Win98; zh-CN; rv:0.9.2) Gecko/20010725 Netscape6/6.1"

附注

PS: 建议使用特征检测来检测web 新特性的支持情况,从而实现对浏览器UX的渐进增强的优化!

通过检测 userAgent 的值来判断浏览器类型是不可靠的,也是不推荐的,因为用户可以修改userAgent 的值。比如:

  • Firefox 中,你可以通过在 about:config 页面添加并修改general.useragent.override 选项的值来覆盖默认的用户代理字符串。一些Firefox扩展也可以用其他方式修改这个值,但它们通常只会修改发送到服务器的User-Agent请求头的值,而不会影响 JavaScript 代码中获取到的 window.navigator.userAgent 属性的值。
  • Opera 6+ 中允许用户通过菜单选择不同的用户代理字符串。
  • Microsoft Internet Explorer 使用注册表来配置自己的用户代理字符串。
  • Safari 和 iCab 也允许用户修改其用户代理字符串,来将自己伪装成 Internet Explorer 或 Netscape。

规范

DOM Level 0 不属于任何标准.

相关链接

文档标签和贡献者

 此页面的贡献者: xgqfrms, teoli, khalid32, AlexChao, ziyunfei
 最后编辑者: xgqfrms,