Created
February 27, 2016 09:22
-
-
Save attitude/16fefb337abc10233549 to your computer and use it in GitHub Desktop.
Platform CSS class on HTML tag
This file contains hidden or 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
/** | |
* 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