使用 JavaScript 或 jQuery 检测 MacOSX 或 Windows 计算机的最佳方法
所以我尝试移动一个“关闭”按钮到左侧当用户在 Mac 上和右侧当用户在 PC 上。现在我通过检查用户代理来完成这项工作,但是它太容易受到欺骗,无法进行可靠的操作系统检测。有没有一个万无一失的方法来检测运行浏览器的操作系统是 Mac OS X 还是 Windows?如果没有,还有什么比用户代理嗅探更好的呢?
Mac68KMacintosh 68K 系统。 MacPPC < em > Macintosh PowerPC 系统。
麦金塔英特尔系统。 MacIntel < em > 苹果硅(ARM)
IOS 设备
iPhoneIPhone.
IPodTouch.
平板电脑。
现代的 mac 返回 navigator.platform == "MacIntel",但是为了给一些“未来的证明”不使用精确匹配,希望他们将来会变成像 MacARM或 MacQuantum这样的东西。
var isMac = navigator.platform.toUpperCase().indexOf('MAC')>=0;
包括同样使用“左边”的 iOS 操作系统
var isMacLike = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
var isIOS = /(iPhone|iPod|iPad)/i.test(navigator.platform);
var is_OSX = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
var is_iOS = /(iPhone|iPod|iPad)/i.test(navigator.platform);
var is_Mac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
var is_iPhone = navigator.platform == "iPhone";
var is_iPod = navigator.platform == "iPod";
var is_iPad = navigator.platform == "iPad";
/* Output */
var out = document.getElementById('out');
if (!is_OSX) out.innerHTML += "This NOT a Mac or an iOS Device!";
if (is_Mac) out.innerHTML += "This is a Mac Computer!\n";
if (is_iOS) out.innerHTML += "You're using an iOS Device!\n";
if (is_iPhone) out.innerHTML += "This is an iPhone!";
if (is_iPod) out.innerHTML += "This is an iPod Touch!";
if (is_iPad) out.innerHTML += "This is an iPad!";
out.innerHTML += "\nPlatform: " + navigator.platform;
<pre id="out"></pre>
Since most O.S. use the close button on the right, you can just move the close button to the left when the user is on a MacLike O.S., otherwise isn't a problem if you put it on the most common side, the right.
setTimeout(test, 1000); //delay for demonstration
function test() {
var mac = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
if (mac) {
document.getElementById('close').classList.add("left");
}
}
<div id="window">
<div id="close">x</div>
<p>Hello!</p>
<p>If the "close button" change to the left side</p>
<p>you're on a Mac like system!</p>
</div>
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().toLowerCase();
let isOSX = /mac/.test(platform); // Mac desktop
let isIOS = ['iphone', 'ipad', 'ipod'].indexOf(platform); // Mac iOs
let isApple = isOSX || isIOS; // Apple device (desktop or iOS)
let isWindows = /win/.test(platform); // Windows
let isAndroid = /android/.test(platform); // Android
let isLinux = /linux/.test(platform); // Linux