Last active
September 1, 2016 13:02
-
-
Save lyquix-owner/3bd915c3c4223aa538400270d0b2b528 to your computer and use it in GitHub Desktop.
getOS: function that returns the name and version of the operating system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// returns the os name, type and version, and sets body classes | |
// detects major desktop and mobile os: Windows, Windows Phone, Mac, iOS, Android, Ubuntu, Fedora, ChromeOS | |
// based on bowser: https://github.com/ded/bowser | |
// list of user agent strings: http://www.webapps-online.com/online-tools/user-agent-strings/dv | |
function getOS() { | |
var ua = navigator.userAgent, os; | |
// helper functions to deal with common regex | |
function getFirstMatch(regex) { | |
var match = ua.match(regex); | |
return (match && match.length > 1 && match[1]) || ''; | |
} | |
function getSecondMatch(regex) { | |
var match = ua.match(regex); | |
return (match && match.length > 1 && match[2]) || ''; | |
} | |
if(/(ipod|iphone|ipad)/i.test(ua)) { | |
os = { | |
name: 'iOS', | |
type: 'ios', | |
version: getFirstMatch(/os (\d+([_\s]\d+)*) like mac os x/i).replace(/[_\s]/g, '.') | |
} | |
} else if(/windows phone/i.test(ua)) { | |
os = { | |
name: 'Windows Phone', | |
type: 'windowsphone', | |
version: getFirstMatch(/windows phone (?:os)?\s?(\d+(\.\d+)*)/i) | |
} | |
} else if(!(/like android/i.test(ua)) && /android/i.test(ua)) { | |
os = { | |
name: 'Android', | |
type: 'android', | |
version: getFirstMatch(/android[ \/-](\d+(\.\d+)*)/i) | |
} | |
} else if(/windows nt/i.test(ua)) { | |
os = { | |
name: 'Windows', | |
type: 'windows', | |
version: getFirstMatch(/windows nt (\d+(\.\d+)*)/i) | |
} | |
} else if(/mac os x/i.test(ua)) { | |
os = { | |
name: 'Mac OS X', | |
type: 'macosx', | |
version: getFirstMatch(/mac os x (\d+([_\s]\d+)*)/i).replace(/[_\s]/g, '.') | |
} | |
} else if(/ubuntu/i.test(ua)) { | |
os = { | |
name: 'Ubuntu', | |
type: 'ubuntu', | |
version: getFirstMatch(/ubuntu\/(\d+(\.\d+)*)/i) | |
} | |
} else if(/fedora/i.test(ua)) { | |
os = { | |
name: 'Fedora', | |
type: 'fedora', | |
version: getFirstMatch(/fedora\/(\d+(\.\d+)*)/i) | |
} | |
} else if(/CrOS/.test(ua)) { | |
os = { | |
name: 'Chrome OS', | |
type: 'chromeos', | |
version: getSecondMatch(/cros (.+) (\d+(\.\d+)*)/i) | |
} | |
} | |
return os; | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment