Skip to content

Instantly share code, notes, and snippets.

@happyguohua
Created May 28, 2015 01:07
Show Gist options
  • Select an option

  • Save happyguohua/cb3efd71c9eefa363aa6 to your computer and use it in GitHub Desktop.

Select an option

Save happyguohua/cb3efd71c9eefa363aa6 to your computer and use it in GitHub Desktop.
get os platform from browser
/*
* will return one of the following value
* [Windows Phone] // windows phone
* [Android] // Android device
* [Linux] // Centos, Ubuntu, Amazon Kindle...
* [iOS] // ipad, iphone
* [OS X] // macbook
* [Windows] // Windows
* [''] // others
*/
getOsPlatform: function getOsPlatform() {
// order is important!
var guesses = [
'Windows Phone',
'Android',
'Linux',
'Mac OS X',
'Macintosh',
'Mac',
'Windows'
],
regTrim = /\s+/g,
regIos = /ipad|iphone/i,
ua = window.navigator.userAgent || '',
temp,
result;
for (var i = 0, j = guesses.length; i < j; i++) {
if (result = RegExp(guesses[i].replace(/\s+/g, '\\s+'), 'i').exec(ua)) {
result = result[0].replace(regTrim, ' ');
break;
}
}
temp = (result || '').toLowerCase();
// figure out if it is iphone/ipad or macbook
return temp === 'mac os x' || temp === 'macintosh' || temp === 'mac' ?
regIos.test(ua) ? 'iOS' : 'OS X' :
result || '';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment