Skip to content

JavaScript Navigator 对象

The window.navigator object contains information about the visitor’s browser and operating system.

The window.navigator object can be accessed directly as navigator.

它提供了各种 properties(属性),但其中许多(特别是与浏览器名称和版本相关的)可能会产生误导,不应依赖它们进行可靠的浏览器检测。Feature detection(特性检测)是一种更可靠的方法。

一些常用的 properties(属性):

  • navigator.userAgent:一个字符串,包含浏览器的 user-agent(用户代理)头信息。
  • navigator.language:一个字符串,表示用户的首选语言,通常是浏览器界面的语言。
  • navigator.languages:一个字符串数组,表示用户的首选语言列表,按偏好顺序排序。
  • navigator.cookieEnabled:一个布尔值,指示浏览器中是否启用了 cookies(小型文本文件,用于存储用户设置和浏览器活动等信息)。
  • navigator.onLine:一个布尔值,指示浏览器当前是否连接到互联网。
  • navigator.platform:一个字符串,标识浏览器运行所在的平台(例如,“Win32”、“MacIntel”、“Linux x86_64”)。这也可以被伪造(spoofed)。
Section titled “Cookie 启用状态 (navigator.cookieEnabled)”

navigator.cookieEnabled property(属性)返回 true 表示浏览器中启用了 cookies,否则返回 false。

// 假设有一个id="cookieStatus"的HTML元素
// const cookieStatusElement = document.getElementById("cookieStatus");
// if (cookieStatusElement) {
// cookieStatusElement.textContent = "Cookies Enabled: " + navigator.cookieEnabled;
// }
console.log("Cookies Enabled: " + navigator.cookieEnabled);

User Agent 字符串 (navigator.userAgent)

Section titled “User Agent 字符串 (navigator.userAgent)”

navigator.userAgent property(属性)返回浏览器发送给服务器的 user-agent 字符串。

// const userAgentElement = document.getElementById("userAgentInfo");
// if (userAgentElement) {
// userAgentElement.textContent = "User Agent: " + navigator.userAgent;
// }
console.log("User Agent: " + navigator.userAgent);

警告:user-agent 字符串非常复杂,用户或浏览器扩展可以轻松地伪造它。强烈不建议依赖它来检测特定的浏览器、版本或特性。请改为使用 feature detection(特性检测)。

传统浏览器标识 Properties(属性)(通常不可靠)

Section titled “传统浏览器标识 Properties(属性)(通常不可靠)”

像 navigator.appName、navigator.appCodeName、navigator.appVersion 和 navigator.product 这样的旧 properties 存在是出于历史原因,但它们提供的关于现代浏览器的信息很少且不可靠。

例如:

  • navigator.appName 通常会返回 “Netscape”,这是出于兼容性原因,即使在 Chrome、Firefox 和 Safari 等现代浏览器中也是如此。
  • navigator.appCodeName 出于类似的历史原因通常返回 “Mozilla”。

最好避免在任何关键逻辑中使用这些 properties。

navigator.platform property(属性)返回一个字符串,标识浏览器运行所在的平台。

// const platformElement = document.getElementById("platformInfo");
// if (platformElement) {
// platformElement.textContent = "Platform: " + navigator.platform;
// }
console.log("Platform: " + navigator.platform);

与 userAgent 类似,这些信息也可以被伪造,应谨慎使用。

浏览器语言 (navigator.language 和 navigator.languages)

Section titled “浏览器语言 (navigator.language 和 navigator.languages)”

navigator.language property(属性)返回一个字符串,表示用户的首选语言(例如,“en-US”,“fr-FR”)。

navigator.languages property(属性)返回一个字符串数组,表示用户的首选语言列表,按偏好顺序排列。这个通常更全面。

// const languageElement = document.getElementById("languageInfo");
// if (languageElement) {
// languageElement.textContent = "Preferred Language: " + navigator.language +
// "; All Preferred: " + navigator.languages.join(', ');
// }
console.log("Preferred Language: " + navigator.language);
console.log("All Preferred Languages: " + navigator.languages.join(', '));

navigator.onLine property(属性)返回一个布尔值,指示浏览器当前是否连接到网络 (true),或未连接 (false)。

这个 property 反映了浏览器对网络状态的理解,并且可以动态更新。你还可以监听 window 对象上的 online 和 offline events(事件)。

// const onlineStatusElement = document.getElementById("onlineStatus");
// function updateOnlineStatus() {
// if (onlineStatusElement) {
// onlineStatusElement.textContent = "Online: " + navigator.onLine;
// }
// console.log("Online: " + navigator.onLine);
// }
// updateOnlineStatus();
// window.addEventListener('online', updateOnlineStatus);
// window.addEventListener('offline', updateOnlineStatus);

注意:navigator.onLine 只指示浏览器是否 可以 访问网络(例如,是否存在活动中的网络接口)。它不保证连接到特定的服务器或互联网本身。