admin管理员组文章数量:1313746
I have scoured everywhere on the internet as to how to detect the OS and it's version. I have found out how to do it for windows, (see code below), and now I want it to work for Mac too.
Windows detection code (works perfectly!):
// 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);
So I'm wondering if it's possible to do this SAME thing for Mac OS X. Like,
...
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...
Any ideas? Any help would be much appreciated!
I have scoured everywhere on the internet as to how to detect the OS and it's version. I have found out how to do it for windows, (see code below), and now I want it to work for Mac too.
Windows detection code (works perfectly!):
// 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);
So I'm wondering if it's possible to do this SAME thing for Mac OS X. Like,
...
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...
Any ideas? Any help would be much appreciated!
Share Improve this question asked Sep 16, 2012 at 22:50 ModernDesignerModernDesigner 7,71711 gold badges36 silver badges42 bronze badges 1-
1
Windows detection code (works perfectly!)
perhaps for certain browsers in their default configurations. There is no standard for user agent strings, user agents can report whatever they want, it's unreliable and certainly not "perfect". – RobG Commented Sep 16, 2012 at 23:04
2 Answers
Reset to default 3return {
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),
};
useragent for mac e.g.
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
Yeah it's because on Firefox the OSX Version is not listed as 10_6 but 10.6 So you have to add that specific line : isMac106: /Mac OS X 10.6/.test(userAgent)
Pay attention to the dot between 10 and 6
本文标签: macosHow to Detect Mac OS X Version in JavaScriptStack Overflow
版权声明:本文标题:macos - How to Detect Mac OS X Version in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741881519a2402773.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论