Created
June 20, 2014 08:57
-
-
Save Takazudo/817c43d0456cce05bbb2 to your computer and use it in GitHub Desktop.
html class tweaking
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
/** | |
* `html` class tweaking | |
*/ | |
(function() { | |
var html = document.getElementsByTagName("html")[0]; | |
/* helpers */ | |
var hasClass = function(node, cls) { | |
return new RegExp("(\\s|^)" + cls + "(\\s|$)").test(node.className); | |
}; | |
var addClass = function(node, cls) { | |
if (hasClass(node, cls)) { | |
return; | |
} | |
return node.className = "" + node.className + " " + cls; | |
}; | |
var removeClass = function(node, cls) { | |
if (!hasClass(node, cls)) { | |
return; | |
} | |
node.className = node.className | |
.replace(new RegExp("(\\s|^)" + cls + "(\\s|$)"), " ") | |
.replace(/\s+/g, " ").replace(/^\s|\s$/, ""); | |
}; | |
/* UA detection */ | |
var ua = (function(){ | |
var ua = {}; | |
var navigator = window.navigator; | |
var platforms = [ | |
{ property: 'platform', regex: /iPhone/i, identity: 'iPhone' }, | |
{ property: 'platform', regex: /iPod/i, identity: 'iPod' }, | |
{ property: 'userAgent', regex: /iPad/i, identity: 'iPad' }, | |
{ property: 'userAgent', regex: /Blackberry/i, identity: 'Blackberry' }, | |
{ property: 'userAgent', regex: /Android/i, identity: 'Android' }, | |
{ property: 'platform', regex: /Mac/i, identity: 'Mac' }, | |
{ property: 'platform', regex: /Win/i, identity: 'Windows' }, | |
{ property: 'platform', regex: /Linux/i, identity: 'Linux' } | |
]; | |
for(var i=0, l=platforms.length, platform; i<l; i++){ | |
platform = platforms[i]; | |
ua[platform.identity] = platform.regex.test(navigator[platform.property]); | |
} | |
return ua; | |
})(); | |
/* do it */ | |
if(ua.Windows) { | |
addClass(html, 'os-win'); | |
} | |
if(ua.Mac) { | |
addClass(html, 'os-mac'); | |
} | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment