使用JavaScript或jQuery检测Mac OS X或Windows计算机的最佳方法


110

因此,我尝试在Mac上将“关闭”按钮移至左侧,在PC上将“关闭”按钮移至右侧。现在,我通过检查用户代理来做到这一点,但是对于可靠的OS检测来说,它很容易被欺骗。有没有一种确定方法来检测运行浏览器的操作系统是Mac OS X还是Windows?如果不是,那比用户代理嗅探更好?


20
如果用户操纵用户代理,那不是他或她的问题吗?当担心让他们拥有无效的用户代理(例如,当它使他们能够访问您不希望他们拥有的东西)对您造成伤害时,我会为此担心,但是对于这样的事情,您为什么要强调呢?让他们朝自己的脚开枪,必须处理后果-伴侣,你的背上没有汗水。
Mahmoud Al-Qudsi'5

好吧,更像是一个提示而不是答案。您可以使用条件注释检测IE。这是Windows检测武库的+1。但是如果IE在另一个操作系统(例如Linux上的Wine)的仿真器中运行,这将失败。顺便问一下,Linux呢?
约瑟夫

@ MahmoudAl-Qudsi即使没有进行欺骗,移动Firefox也经常假装它是Safari,Opera在某些版本中常常假装它是Firefox。如果不进行欺骗,则用户代理仍然非常不可靠。
alt 2012年


但是,这个问题的答案仅仅是“用户代理”。
alt 2012年

Answers:


192

window.navigator.platform时的userAgent字符串改变没有欺骗性质。如果将userAgent更改为iPhone或Chrome Windows,则在Mac上进行了测试,navigator.platform仍为MacIntel。

更改userAgent字符串时,不会欺骗navigator.platform

该属性也是只读的

navigator.platform是只读的


我可以拿出下表

Mac电脑

Mac68K Macintosh 68K系统。
MacPPC Macintosh PowerPC系统。
MacIntel Macintosh Intel系统。

iOS设备

iPhone 苹果手机。
iPod iPod Touch。
iPad iPad。


现代Mac回归了,navigator.platform == "MacIntel"但是为了提供一些“未来证明”,请不要使用精确匹配,希望它们会更改为类似MacARMMacQuantum将来的版本。

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/


3
@ Vitim.us非常感谢您提供详细的答案,您真的挽救了我的一天:)
vivekkupadhyay 16/09/28


1
MacQuantum让我开心。😂
ÍhorMé17年

该平台属性是只读的,但它仍然是可能的欺骗吧:stackoverflow.com/questions/2166540/...
瑞安波比吉

1
@Qix一个更好的改进是更换str.match(regexp) ? true : falseregexp.test(string)。该RegExp.prototype.test()方法本地返回一个布尔值。因此,我的首选代码是const platformIsMacLike = /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
罗里·奥肯

52

就这么简单:

function isMacintosh() {
  return navigator.platform.indexOf('Mac') > -1
}

function isWindows() {
  return navigator.platform.indexOf('Win') > -1
}

您可以做一些有趣的事情,例如:

var isMac = isMacintosh();
var isPC = !isMacintosh();

1
好,涵盖了两个操作系统。这里有一个更全面的(但可能仍然是不完整)列表,其中包含的Linux,BSD,机器人,棕榈,索尼Playstation游戏机等:stackoverflow.com/questions/19877924/...
迈克尔Scheper

如果您想涵盖更多的操作系统,则应该使用类似以下的库Platform.jsgithub.com/bestiejs/platform.js
Benny Neugebauer

@BennyNeugebauer isPC不应该等于!isMacintosh();。如果用户使用linux或其他平台,该怎么办?它将检测到它们不在Mac上,并认为它们在PC上。
神秘

@EternalDarkness这就是我之所以称呼它的原因isPC(而不是isWindowsor isLinux),因为Linux在PC上运行,而macOS仅在Mac上运行。
Benny Neugebauer

5

这是你想要的?否则,请告诉我,我将删除此帖子。

试试这个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


2
杜德!我欺骗了用户代理,它仍然在Mac的Safari上检测到我!谢谢BRUV。
alt 2012年

1
@JacksonGariety不用担心,bruv :))在这里阅读更多详细信息,其中包含完整的代码resinding :) stoimen.com/jquery.client.plugin/jquery.client.js有一个不错的brosniac :)干杯!
Tats_innit 2012年

@Derek hiya bruv-它使用quirkmode浏览器插件并检测浏览器/操作系统,请参见quirksmode.org/js/detect.html,并进一步参见stoimen.com/jquery.client.plugin/jquery.client.jsquirksmode.org /js/support.html希望这有意义
Tats_innit 2012年

没关系,这似乎只是在使用用户代理...有点烂。
alt 2012年

1
并不是我要找的答案。可能吗?
ALT

0

让我知道这个是否奏效。在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
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.