因此,我尝试在Mac上将“关闭”按钮移至左侧,在PC上将“关闭”按钮移至右侧。现在,我通过检查用户代理来做到这一点,但是对于可靠的OS检测来说,它很容易被欺骗。有没有一种确定方法来检测运行浏览器的操作系统是Mac OS X还是Windows?如果不是,那比用户代理嗅探更好?
因此,我尝试在Mac上将“关闭”按钮移至左侧,在PC上将“关闭”按钮移至右侧。现在,我通过检查用户代理来做到这一点,但是对于可靠的OS检测来说,它很容易被欺骗。有没有一种确定方法来检测运行浏览器的操作系统是Mac OS X还是Windows?如果不是,那比用户代理嗅探更好?
Answers:
该window.navigator.platform时的userAgent字符串改变没有欺骗性质。如果将userAgent更改为iPhone或Chrome Windows,则在Mac上进行了测试,navigator.platform仍为MacIntel。

该属性也是只读的

我可以拿出下表
Mac电脑
Mac68KMacintosh 68K系统。
MacPPCMacintosh PowerPC系统。
MacIntelMacintosh Intel系统。iOS设备
iPhone苹果手机。
iPodiPod Touch。
iPadiPad。
现代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);
由于大多数操作系统使用右侧的关闭按钮,因此当用户使用MacLike操作系统时,只需将关闭按钮移至左侧,否则,如果将其放在最常见的一侧(右侧),则不会有问题。
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");
}
}
#window {
position: absolute;
margin: 1em;
width: 300px;
padding: 10px;
border: 1px solid gray;
background-color: #DDD;
text-align: center;
box-shadow: 0px 1px 3px #000;
}
#close {
position: absolute;
top: 0px;
right: 0px;
width: 22px;
height: 22px;
margin: -12px;
box-shadow: 0px 1px 3px #000;
background-color: #000;
border: 2px solid #FFF;
border-radius: 22px;
color: #FFF;
text-align: center;
font: 14px"Comic Sans MS", Monaco;
}
#close.left{
left: 0px;
}
<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>
http://www.nczonline.net/blog/2007/12/17/don-t-forget-navigator-platform/
str.match(regexp) ? true : false用regexp.test(string)。该RegExp.prototype.test()方法本地返回一个布尔值。因此,我的首选代码是const platformIsMacLike = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);。
就这么简单:
function isMacintosh() {
return navigator.platform.indexOf('Mac') > -1
}
function isWindows() {
return navigator.platform.indexOf('Win') > -1
}
您可以做一些有趣的事情,例如:
var isMac = isMacintosh();
var isPC = !isMacintosh();
Platform.js:github.com/bestiejs/platform.js
isPC不应该等于!isMacintosh();。如果用户使用linux或其他平台,该怎么办?它将检测到它们不在Mac上,并认为它们在PC上。
isPC(而不是isWindowsor isLinux),因为Linux在PC上运行,而macOS仅在Mac上运行。
这是你想要的?否则,请告诉我,我将删除此帖子。
试试这个jQuery插件:http : //archive.plugins.jquery.com/project/client-detect
演示: http ://www.stoimen.com/jquery.client.plugin/
这基于quirksmode BrowserDetect jQuery浏览器/操作系统检测插件的包装。
对于热衷的读者:
http : //www.stoimen.com/blog/2009/07/16/jquery-browser-and-os-detection-plugin/
http://www.quirksmode.org/js/support.html
围绕该插件的更多代码位于:http : //www.stoimen.com/jquery.client.plugin/jquery.client.js
让我知道这个是否奏效。在StackOverflow.com的帮助下检测Apple设备(Mac计算机,iPhone等)的方法:到目前为止
,navigator.platform的可能值列表是什么?
var deviceDetect = navigator.platform;
var appleDevicesArr = ['MacIntel', 'MacPPC', 'Mac68K', 'Macintosh', 'iPhone',
'iPod', 'iPad', 'iPhone Simulator', 'iPod Simulator', 'iPad Simulator', 'Pike
v7.6 release 92', 'Pike v7.8 release 517'];
// If on Apple device
if(appleDevicesArr.includes(deviceDetect)) {
// Execute code
}
// If NOT on Apple device
else {
// Execute code
}