如何检测的Mac OS X版本在JavaScript(How to Detect Mac OS X

2019-09-22 12:34发布

我到处都走遍了互联网上关于如何检测OS和它的版本。 我已经找到了如何做到这一点的窗口(见下面的代码),现在我希望它为Mac工作了。

的Windows代码检测(完美的作品!):

// OS detection
var _os_ = (function(){
var userAgent = navigator.userAgent.toLowerCase();
return {
    isWin2K: /windows nt 5.0/.test(userAgent),
    isXP: /windows nt 5.1/.test(userAgent),
    isVista: /windows nt 6.0/.test(userAgent),
    isWin7: /windows nt 6.1/.test(userAgent),
};
}());

// get OS shorthand names

var OS;
if(_os_.isWin2K){
OS = "Windows 2000";
}

if(_os_.isXP){
OS = "Windows XP";
}

if(_os_.isVista){
OS = "Windows Vista";
}

if(_os_.isWin7){
OS = "Windows 7";
}

alert(OS);

所以,我想知道是否有可能做同样的事情在Mac OS X一样,

 ...
 return {
     isMac10.5: /mac osx 10.5/.test(userAgent),
     isMac10.6: /mac osx 10.6/.test(userAgent),
     isMac10.7: /mac osx 10.7/.test(userAgent),
     isMac10.8: /mac osx 10.8/.test(userAgent),
 };

 ....
 if(_os_.isMac10.5){
 OS = "Mac OS X Leopard";
 }

 etc., etc...

有任何想法吗? 任何帮助将非常感激!

Answer 1:

return {
     isMac105: /Mac OS X 10_5/.test(userAgent),
     isMac106: /Mac OS X 10_6/.test(userAgent),
     isMac107: /Mac OS X 10_7/.test(userAgent),
     isMac108: /Mac OS X 10_8/.test(userAgent),
 };

用户代理的Mac如

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_1) AppleWebKit/536.25 (KHTML, like Gecko) Version/6.0 Safari/536.25

Macintosh; U; Intel Mac OS X 10_5_8; ru) AppleWebKit/533.18.1 (KHTML, like Gecko) Version/5.0.2 Safari/533.18.5



Answer 2:

是的,那是因为在Firefox上OSX版本没有被列为10_6,但10.6所以,你必须补充的是具体线路:isMac106:/ Mac的OS X 10.6 /。测试(的userAgent)

10和6之间注意点



文章来源: How to Detect Mac OS X Version in JavaScript