Skip to content

Instantly share code, notes, and snippets.

@ttilley
Created March 8, 2011 03:23
Show Gist options
  • Save ttilley/859799 to your computer and use it in GitHub Desktop.
Save ttilley/859799 to your computer and use it in GitHub Desktop.
plain javascript port of dojo's uacss module
(function(){
var state = {};
var n = navigator,
ua = n.userAgent,
av = n.appVersion,
tv = parseFloat(av);
if(ua.indexOf("Opera") >= 0){state.isOpera = tv;}
state.isKhtml = (av.indexOf("Konqueror") >= 0) ? tv : 0;
state.isWebKit = parseFloat(ua.split("WebKit/")[1]) || undefined;
state.isChrome = parseFloat(ua.split("Chrome/")[1]) || undefined;
state.isMac = ua.indexOf("Macintosh") >= 0;
state.isLinux = ua.indexOf("Linux") >= 0;
state.isWindows = ua.indexOf("Windows") >= 0;
var index = Math.max(av.indexOf("WebKit"), av.indexOf("Safari"), 0);
if(index && !state.isChrome){
state.isSafari = parseFloat(av.split("Version/")[1]);
if(!state.isSafari || parseFloat(av.substr(index + 7)) <= 419.3){
state.isSafari = 2;
}
}
if(ua.indexOf("Gecko") >= 0 && !state.isKhtml && !state.isWebKit){state.isMozilla = state.isMoz = tv;}
if(state.isMoz){
state.isFF = parseFloat(ua.split("Firefox/")[1] || ua.split("Minefield/")[1]) || undefined;
}
if(document.all && !state.isOpera){
state.isIE = parseFloat(av.split("MSIE ")[1]) || undefined;
var mode = document.documentMode;
if(mode && mode != 5 && Math.floor(state.isIE) != mode){
state.isIE = mode;
}
}
state.isQuirks = document.compatMode == "BackCompat";
state.boxModel = "content-box";
if (state.isIE) {
state.boxModel = state.isQuirks ? "border-box" : "content-box";
}
var html = window.document.documentElement,
maj = Math.floor,
boxModel = state.boxModel.replace(/-/,''),
classes = {
ie: state.isIE,
ie6: maj(state.isIE) == 6,
ie7: maj(state.isIE) == 7,
ie8: maj(state.isIE) == 8,
ie9: maj(state.isIE) == 9,
quirks: state.isQuirks,
iequirks: state.isIE && state.isQuirks,
opera: state.isOpera,
khtml: state.isKhtml,
webkit: state.isWebKit,
safari: state.isSafari,
chrome: state.isChrome,
gecko: state.isMozilla,
ff2: maj(state.isFF) == 2,
ff3: maj(state.isFF) == 3,
ff4: maj(state.isFF) == 4,
mac: state.isMac,
linux: state.isLinux,
windows: state.isWindows
};
classes[state.boxModel] = true;
var classStr = "";
for(var clz in classes){
if(classes[clz]){
classStr += clz + " ";
}
}
var trim = String.prototype.trim ?
function(str){ return str.trim(); } :
function(str){ return str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''); };
html.className = trim(html.className + " " + classStr);
})();
(function(){var a={},b=navigator,c=b.userAgent,d=b.appVersion,e=parseFloat(d);c.indexOf("Opera")>=0&&(a.isOpera=e),a.isKhtml=d.indexOf("Konqueror")>=0?e:0,a.isWebKit=parseFloat(c.split("WebKit/")[1])||undefined,a.isChrome=parseFloat(c.split("Chrome/")[1])||undefined,a.isMac=c.indexOf("Macintosh")>=0,a.isLinux=c.indexOf("Linux")>=0,a.isWindows=c.indexOf("Windows")>=0;var f=Math.max(d.indexOf("WebKit"),d.indexOf("Safari"),0);if(f&&!a.isChrome){a.isSafari=parseFloat(d.split("Version/")[1]);if(!a.isSafari||parseFloat(d.substr(f+7))<=419.3)a.isSafari=2}c.indexOf("Gecko")>=0&&!a.isKhtml&&!a.isWebKit&&(a.isMozilla=a.isMoz=e),a.isMoz&&(a.isFF=parseFloat(c.split("Firefox/")[1]||c.split("Minefield/")[1])||undefined);if(document.all&&!a.isOpera){a.isIE=parseFloat(d.split("MSIE ")[1])||undefined;var g=document.documentMode;g&&g!=5&&Math.floor(a.isIE)!=g&&(a.isIE=g)}a.isQuirks=document.compatMode=="BackCompat",a.boxModel="content-box",a.isIE&&(a.boxModel=a.isQuirks?"border-box":"content-box");var h=window.document.documentElement,i=Math.floor,j=a.boxModel.replace(/-/,""),k={ie:a.isIE,ie6:i(a.isIE)==6,ie7:i(a.isIE)==7,ie8:i(a.isIE)==8,ie9:i(a.isIE)==9,quirks:a.isQuirks,iequirks:a.isIE&&a.isQuirks,opera:a.isOpera,khtml:a.isKhtml,webkit:a.isWebKit,safari:a.isSafari,chrome:a.isChrome,gecko:a.isMozilla,ff2:i(a.isFF)==2,ff3:i(a.isFF)==3,ff4:i(a.isFF)==4,mac:a.isMac,linux:a.isLinux,windows:a.isWindows};k[a.boxModel]=!0;var l="";for(var m in k)k[m]&&(l+=m+" ");var n=String.prototype.trim?function(a){return a.trim()}:function(a){return a.replace(/^\s\s*/,"").replace(/\s\s*$/,"")};h.className=n(h.className+" "+l)})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment