JavaScript Window Navigator

本文介绍了如何通过navigator对象获取浏览器代码名、名称、版本、Cookies启用状态、平台信息及User-Agent头部等关键信息,强调了这些信息可能存在的误导性,并提供了一种使用对象检测进行浏览器检测的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

window.navigator 对象包含有关访问者浏览器的信息。

Window Navigator

window.navigator 对象在编写时可不使用 window 这个前缀。

实例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<div id="example"></div>
<script>
	txt = "<p>Browser CodeName:" + navigator.appCodeName + "</p>";
	txt += "<p>Browser Name:" + navigator.appName + "</p>";
	txt += "<p>Browser Version:" + navigator.appVersion + "</p>";
	txt += "<p>Cookies Enabled:" + navigator.cookieEnabled +"</p>";
	txt += "<p>platform:" + navigator.platform + "</p>";
	txt += "<p>User-agent header:" + navigator.userAgent + "</p>";
	txt += "<p>User-agent language:" + navigator.systemLanguage + "</p>";
	document.getElementById("example").innerHTML = txt;
</script>
</body>
</html>
效果图:


注意:来自 navigator 对象的信息具有误导性,不应该被用于检测浏览器版本,这是因为:

  • navigator 数据可被浏览器使用者更改
  • 浏览器无法报告晚于浏览器发布的新操作系统

浏览器检测

由于 navigator 可误导浏览器检测,使用对象检测可用来嗅探不同的浏览器。

由于不同的浏览器支持不同的对象,您可以使用对象来检测浏览器。例如,由于只有 Opera 支持属性 "window.opera",您可以据此识别出 Opera。

例子:if (window.opera) {...some action...}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值