Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Detect Mac OS X Version in JavaScript

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!

like image 311
ModernDesigner Avatar asked Sep 16 '12 22:09

ModernDesigner


People also ask

How to detect Mac os version in JavaScript?

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

Can JavaScript detect OS?

In JavaScript, the navigator object returns the browser information. Technically, through the navigator properties, we send information about the browser and sometimes information about the app or operating system we are using to the server.

How do I find my browser OS?

To detect the operating system on the client machine, one can simply use navigator. appVersion or navigator. userAgent property. The Navigator appVersion property is a read-only property and it returns a string which represents the version information of the browser.


2 Answers

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),
 };

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

like image 79
askovpen Avatar answered Sep 22 '22 13:09

askovpen


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

like image 35
anonymous Avatar answered Sep 23 '22 13:09

anonymous