var OSName = "Unknown";
if (window.navigator.userAgent.indexOf("Windows NT 10.0")!= -1) OSName="Windows 10";
if (window.navigator.userAgent.indexOf("Windows NT 6.3") != -1) OSName="Windows 8.1";
if (window.navigator.userAgent.indexOf("Windows NT 6.2") != -1) OSName="Windows 8";
if (window.navigator.userAgent.indexOf("Windows NT 6.1") != -1) OSName="Windows 7";
if (window.navigator.userAgent.indexOf("Windows NT 6.0") != -1) OSName="Windows Vista";
if (window.navigator.userAgent.indexOf("Windows NT 5.1") != -1) OSName="Windows XP";
if (window.navigator.userAgent.indexOf("Windows NT 5.0") != -1) OSName="Windows 2000";
if (window.navigator.userAgent.indexOf("Mac") != -1) OSName="Mac/iOS";
if (window.navigator.userAgent.indexOf("X11") != -1) OSName="UNIX";
if (window.navigator.userAgent.indexOf("Linux") != -1) OSName="Linux";
// on IE10 x86 platform preview running in IE7 compatibility mode on Windows 7 64 bit edition
platform.name; // 'IE'
platform.version; // '10.0'
platform.layout; // 'Trident'
platform.os; // 'Windows Server 2008 R2 / 7 x64'
platform.description; // 'IE 10.0 x86 (platform preview; running in IE 7 mode) on Windows Server 2008 R2 / 7 x64'
// or on an iPad
platform.name; // 'Safari'
platform.version; // '5.1'
platform.product; // 'iPad'
platform.manufacturer; // 'Apple'
platform.layout; // 'WebKit'
platform.os; // 'iOS 5.0'
platform.description; // 'Safari 5.1 on Apple iPad (iOS 5.0)'
// or parsing a given UA string
var info = platform.parse('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7.2; en; rv:2.0) Gecko/20100101 Firefox/4.0 Opera 11.52');
info.name; // 'Opera'
info.version; // '11.52'
info.layout; // 'Presto'
info.os; // 'Mac OS X 10.7.2'
info.description; // 'Opera 11.52 (identifying as Firefox 4.0) on Mac OS X 10.7.2'
我不能评论@Ian Ippolito的回答(因为如果我有代表的话我会评论的),但根据他的评论链接,我相当确定你可以找到IOS的Chrome版本。https://developer.chrome.com/multidevice/user-agent?hl=ja列出UA为:Mozilla/5.0 (iPhone;CPU iPhone OS 10_3像Mac OS X) AppleWebKit/602.1.50 (KHTML,像壁虎)CriOS/56.0.2924.75移动/14E5239e Safari/602.1
所以这应该是可行的:
if ((verOffset = nAgt.indexOf('CriOS')) != -1) {
//Chrome on iPad spoofing Safari...correct it.
browser = 'Chrome';
version = nAgt.substring(verOffset + 6);//should get the criOS ver.
}
同样,如果你想创建一个本地NodeJS应用也是可以的!只需使用const os = require("os");调用os模块。然后在控制台中记录常量。你将得到一个完整的对象,但如果你想查看平台名称,你可以输入console.log(os.platform());,并确保在os.platform后添加一对括号,因为platform这里是一个函数!
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