Skip to content

Instantly share code, notes, and snippets.

@attitude
Created February 27, 2016 09:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save attitude/16fefb337abc10233549 to your computer and use it in GitHub Desktop.
Save attitude/16fefb337abc10233549 to your computer and use it in GitHub Desktop.
Platform CSS class on HTML tag
/**
* Platform CSS class on HTML tag inspired by https://github.com/driftyco/ionic/blob/67ef9ebde21ac64e1f9e3db43b16c2db7ed2256d/js/utils/platform.js
*/
(function(w, el) {
var platformName,
IOS = 'ios',
ANDROID = 'android',
WINDOWS_PHONE = 'windowsphone',
EDGE = 'edge',
CROSSWALK = 'crosswalk';
// Fallback
if (!el) {
el = w.document.getElementsByTagName('html')[0];
}
this.setPlatform = function (n) {
if (n.indexOf('Edge') > -1) {
platformName = EDGE;
} else if (n.indexOf('Windows Phone') > -1) {
platformName = WINDOWS_PHONE;
} else if (n.indexOf('Android') > 0) {
platformName = ANDROID;
} else if (/iPhone|iPad|iPod/.test(n)) {
platformName = IOS;
} else {
platformName = w.navigator.platform && w.navigator.platform.toLowerCase().split(' ')[0] || '';
}
}
this.setPlatform(window.navigator.userAgent);
if (platformName) {
el.className += ' platform-' + platformName;
}
}(window, typeof platformTagTarget !== 'undefined' ? platformTagTarget : null));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment