如何使用JavaScript找到操作系统名称和操作系统版本?


当前回答

我正在使用这个:)

getPlatform() {
    const allPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE', 'Android', 'iPhone', 'iPad', 'iPod'];
    return allPlatforms.find(item => item === navigator.platform);
}

其他回答

如前所述,导航器。平台已弃用。我们应该使用导航器。但它仍然缺乏支持(firefox, safari在2022年不支持它)。

注意导航器。userAgentData只适用于通过https兼容的浏览器。

下面是一个脚本,它将使用UserAgentData,并返回到旧的操作方式。

function get_platform() {
    // 2022 way of detecting. Note : this userAgentData feature is available only in secure contexts (HTTPS)
    if (typeof navigator.userAgentData !== 'undefined' && navigator.userAgentData != null) {
        return navigator.userAgentData.platform;
    }
    // Deprecated but still works for most of the browser
    if (typeof navigator.platform !== 'undefined') {
        if (typeof navigator.userAgent !== 'undefined' && /android/.test(navigator.userAgent.toLowerCase())) {
            // android device’s navigator.platform is often set as 'linux', so let’s use userAgent for them
            return 'android';
        }
        return navigator.platform;
    }
    return 'unknown';
}

let platform = get_platform();

// examples of use
let isOSX = /mac/.test(platform.toLowerCase()); // Mac desktop
let isIOS = ['iphone', 'ipad', 'ipod'].indexOf(platform.toLowerCase()) !== -1; // Mac iOs
let isApple = isOSX || isIOS; // Apple device (desktop or iOS)
let isWindows = /win/.test(platform.toLowerCase()); // Windows
let isAndroid = /android/.test(platform.toLowerCase()); // Android
let isLinux = /linux/.test(platform.toLowerCase()); // Linux

JavaScript可以访问window.navigator.platform——一个表示浏览器平台的字符串: https://developer.mozilla.org/en-US/docs/Web/API/NavigatorID/platform

使用它来提取您需要(并且可以)的任何细节。请注意,并不能保证客户端实际使用了该平台,因为这可以很容易地以多种方式进行修改。

试一试:

console.log (window.navigator.platform);

User-Agent字符串对于获取平台信息(OS, OS版本,cpu-arch等)不再可靠,因为可以重写它们,甚至浏览器的默认设置也不准确。 在浏览器控制台中尝试"console.log(navigator)"并检查User-Agent字段。

为了解决上述问题,W3有一个新的提议(https://wicg.github.io/ua-client-hints/),它将允许用户请求关于平台的特定信息。截至2021年5月11日,只有基于铬的浏览器(Chrome、Edge、Opera、Brave……)实现了该规范(该提案仍是非正式的)。

获取平台信息的一种方法是服务器通过向初始响应添加'Accept-CH: Sec-CH-UA-Platform-Version'这样的报头来请求特定的信息,来自客户端的任何后续请求都将包含平台信息报头。欲了解更多信息,请参阅上面的链接。

通过Javascript在客户端获取信息,下面的代码可以使用(复制并粘贴在chrome控制台)。

let platformDetails = await navigator.userAgentData.getHighEntropyValues(["architecture", 
"platform", "platformVersion", "model", "bitness", "uaFullVersion"]);
console.log(platformDetails);

我创建了一个用于解析User Agent字符串的库,名为Voodoo。但是要注意,不应该用它来代替特征检测。

Voodoo所做的是解析userAgent字符串,该字符串在Navigator对象(window.navigator)中找到。并不是所有浏览器都会传递可靠的userAgent字符串,所以即使这是正常的方式,userAgent也不总是可信的。

使用DetectOS.js。这是一个简单的JS定义,适用于没有依赖关系的流行操作系统和浏览器:

class DetectOS { constructor() { this.browser = this.searchString(this.dataBrowser()) this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) this.OS = this.searchString(this.dataOS()) } searchString(data) { for (let i = 0; i < data.length; i++) { let dataString = data[i].string, dataProp = data[i].prop this.versionSearchString = data[i].versionSearch || data[i].identity if (dataString) { if (dataString.indexOf(data[i].subString) !== -1) { return data[i].identity } } else if (dataProp) { return data[i].identity } } } searchVersion(dataString) { let index = dataString.indexOf(this.versionSearchString) if (index === -1) return return parseFloat(dataString.substring(index+this.versionSearchString.length + 1)) } dataBrowser() { return [ /*************** * Chrome ***************/ { string: navigator.userAgent, subString: "Chrome", identity: "Chrome" }, /*************** * Safari ***************/ { string: navigator.vendor, subString: "Apple", identity: "Safari", versionSearch: "Version" }, /*************** * For Older Opera (12.18-) ***************/ { prop: window.opera, identity: "Opera", versionSearch: "Version" }, /*************** * Internet Explorer 10 ***************/ { string: navigator.userAgent, subString: "MSIE", identity: "IE10", versionSearch: "MSIE" }, /*************** * Internet Explorer 11 ***************/ { string: navigator.userAgent, subString: "Trident", identity: "IE11", versionSearch: "rv" }, /*************** * Edge ***************/ { string: navigator.userAgent, subString: "Edge", identity: "Edge", versionSearch: "Edge" }, /*************** * Firefox ***************/ { string: navigator.userAgent, subString: "Firefox", identity: "Firefox" }, { string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv" }, /*************** * For Older Netscapes (4-) ***************/ { string: navigator.userAgent, subString: "Mozilla", identity: "Netscape", versionSearch: "Mozilla" }, /*************** * For Newer Netscapes (6+) ***************/ { string: navigator.userAgent, subString: "Netscape", identity: "Netscape" }, /*************** * Other Browsers ***************/ { string: navigator.userAgent, subString: "OmniWeb", versionSearch: "OmniWeb/", identity: "OmniWeb" }, { string: navigator.vendor, subString: "iCab", identity: "iCab" }, { string: navigator.vendor, subString: "KDE", identity: "Konqueror" }, { string: navigator.vendor, subString: "Camino", identity: "Camino" } ] } dataOS() { return [ { string: navigator.platform, subString: 'Win', identity: 'Windows' }, { string: navigator.platform, subString: 'Mac', identity: 'macOS' }, { string: navigator.userAgent, subString: 'iPhone', identity: 'iOS' }, { string: navigator.userAgent, subString: 'iPad', identity: 'iOS' }, { string: navigator.userAgent, subString: 'iPod', identity: 'iOS' }, { string: navigator.userAgent, subString: 'Android', identity: 'Android' }, { string: navigator.platform, subString: 'Linux', identity: 'Linux' } ] } } const Detect = new DetectOS() console.log("We know your browser – it's " + Detect.browser + " " + Detect.version); console.log("We know your OS – it's " + Detect.OS); console.log("We know everything about you.");