navigator 对象概述
navigator 对象是 window 对象的一部分,因此你可以直接通过 navigator 来访问它。常见的用途包括检测浏览器类型、语言、操作系统信息等。
常用属性和方法
1. navigator.userAgent
返回一个字符串,包含浏览器的用户代理信息。通常用于检测浏览器类型、版本、操作系统等。
console.log(navigator.userAgent);
// 例如:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36
2. navigator.language
返回浏览器的语言设置。返回的值通常是一个符合 BCP 47 标准的语言标识符,例如 “en-US” 或 “zh-CN”。
console.log(navigator.language);
// 例如:en-US
3. navigator.platform
返回浏览器所在操作系统的信息(例如 “Windows”、“MacIntel”、“Linux” 等)。
console.log(navigator.platform);
// 例如:Win32
4. navigator.onLine
返回一个布尔值,指示浏览器是否处于在线状态。如果设备连接到互联网,则返回 true,否则返回 false。
console.log(navigator.onLine);
// 例如:true
5. navigator.cookieEnabled
返回一个布尔值,指示浏览器是否启用了 cookie。
console.log(navigator.cookieEnabled);
// 例如:true
6. navigator.geolocation
提供浏览器的地理位置服务。你可以使用 navigator.geolocation 来获取用户的当前位置(经纬度等)。
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
console.log(position.coords.latitude, position.coords.longitude);
});
} else {
console.log('Geolocation is not supported by this browser.');
}
7. navigator.hardwareConcurrency
返回设备支持的逻辑处理器数量(通常是 CPU 核心数)。
console.log(navigator.hardwareConcurrency);
// 例如:4
8. navigator.maxTouchPoints
返回设备支持的最大触摸点数。对于支持触摸屏的设备,它可以返回支持的最大触摸数量。
console.log(navigator.maxTouchPoints);
// 例如:10
9. navigator.bluetooth(Web Bluetooth API)
访问 Web Bluetooth API,使网页能够与 Bluetooth 设备进行通信(如打印机、耳机等)。
if (navigator.bluetooth) {
console.log("Bluetooth is available!");
} else {
console.log("Bluetooth is not available.");
}
10. navigator.mediaDevices(WebRTC)
提供访问媒体设备(如麦克风、摄像头等)的方法和属性。
if (navigator.mediaDevices) {
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
.then(function(stream) {
console.log('成功获取用户的媒体流');
})
.catch(function(error) {
console.error('获取媒体流失败:', error);
});
}
navigator 对象概述
navigator 对象是 window 对象的一部分,因此你可以直接通过 navigator 来访问它。常见的用途包括检测浏览器类型、语言、操作系统信息等。
常用属性和方法
1. navigator.userAgent
返回一个字符串,包含浏览器的用户代理信息。通常用于检测浏览器类型、版本、操作系统等。
console.log(navigator.userAgent);
// 例如:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36
2. navigator.language
返回浏览器的语言设置。返回的值通常是一个符合 BCP 47 标准的语言标识符,例如 “en-US” 或 “zh-CN”。
console.log(navigator.language);
// 例如:en-US
3. navigator.platform
返回浏览器所在操作系统的信息(例如 “Windows”、“MacIntel”、“Linux” 等)。
console.log(navigator.platform);
// 例如:Win32
4. navigator.onLine
返回一个布尔值,指示浏览器是否处于在线状态。如果设备连接到互联网,则返回 true,否则返回 false。
console.log(navigator.onLine);
// 例如:true
5. navigator.cookieEnabled
返回一个布尔值,指示浏览器是否启用了 cookie。
console.log(navigator.cookieEnabled);
// 例如:true
6. navigator.geolocation
提供浏览器的地理位置服务。你可以使用 navigator.geolocation 来获取用户的当前位置(经纬度等)。
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
console.log(position.coords.latitude, position.coords.longitude);
});
} else {
console.log('Geolocation is not supported by this browser.');
}
7. navigator.hardwareConcurrency
返回设备支持的逻辑处理器数量(通常是 CPU 核心数)。
console.log(navigator.hardwareConcurrency);
// 例如:4
8. navigator.maxTouchPoints
返回设备支持的最大触摸点数。对于支持触摸屏的设备,它可以返回支持的最大触摸数量。
console.log(navigator.maxTouchPoints);
// 例如:10
9. navigator.bluetooth(Web Bluetooth API)
访问 Web Bluetooth API,使网页能够与 Bluetooth 设备进行通信(如打印机、耳机等)。
if (navigator.bluetooth) {
console.log("Bluetooth is available!");
} else {
console.log("Bluetooth is not available.");
}
10. navigator.mediaDevices(WebRTC)
提供访问媒体设备(如麦克风、摄像头等)的方法和属性。
if (navigator.mediaDevices) {
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
.then(function(stream) {
console.log('成功获取用户的媒体流');
})
.catch(function(error) {
console.error('获取媒体流失败:', error);
});
}