Last active
December 14, 2015 22:39
-
-
Save barneycarroll/5160392 to your computer and use it in GitHub Desktop.
Get your /h5bp/-like HTML classnames for IE versions dynamically. Useful if you don't have control of the raw markup as spat out by the server.
This file contains 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
// Optain classnames representing the IE rendering engine: | |
// Makes version-targetted CSS much cleaner | |
var sIEclass = (function determineIEversion(){ | |
// A DOM element to run the test on | |
var oTest = document.createElement('test'); | |
// A table matching tested versions to representative class string | |
var hVersions = { | |
"6" : "ie ltie10 ltie9 ltie8 ltie7 ie6", | |
"7" : "ie ltie10 ltie9 ltie8 ie7", | |
"8" : "ie ltie10 ltie9 ie8", | |
"9" : "ie ltie10 ie9" | |
}; | |
// A conditional comment with `#` as a placeholder for the IE version | |
var sMarkup = '<!--[if IE #]>#<![endif]-->'; | |
// Store any matched versions here | |
var aMatch; | |
// Loop through test candidate versions | |
for(var sVersion in hVersions){ | |
if(hVersions.hasOwnProperty(sVersion)){ | |
// Append a conditional comment for each, targetted to that version, | |
// containing that version number as a text node | |
oTest.innerHTML = oTest.innerHTML + sMarkup.replace(/#/g, sVersion); | |
} | |
} | |
// Non-IE && IE10+ browsers will read the contents as several comments; | |
// Old IEs will see just their respective version number | |
aMatch = oTest.innerHTML.match(/^\d$/); | |
if(aMatch){ | |
// Return the class string corresponding to the retrieved version number | |
return hVersions[aMatch[0]]; | |
} | |
}()); | |
document.lastChild.className += ' ' + sIEclass; |
This file contains 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
// False for IE10 and above, integer otherwise | |
var ieVersion = (function getIEVersion(){ | |
var test = document.createElement('x'); | |
var comment = '<!--[if IE #]>#<![endif]-->'; | |
var versions = 10; | |
while(--versions){ | |
test.innerHTML += comment.replace(/#/g, versions); | |
} | |
return ~~test.innerHTML || false; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment