Skip to content

Instantly share code, notes, and snippets.

@eriwen
Created August 28, 2011 02:04
Show Gist options
  • Save eriwen/1176146 to your computer and use it in GitHub Desktop.
Save eriwen/1176146 to your computer and use it in GitHub Desktop.
JS test for selector support (another brilliant bit from @LeaVerou)
function supportsSelector(selector) {
var el = document.createElement('div');
el.innerHTML = ['&shy;', '<style>', selector, '{}', '</style>'].join('');
el = document.body.appendChild(el);
var style = el.getElementsByTagName('style')[0],
ret = !!(style.sheet.rules || style.sheet.cssRules)[0];
document.body.removeChild(el);
return ret;
}
console.log(':nth-child', supportsSelector(':nth-child(odd)'));
console.log(':unsupported', supportsSelector(':unsupported'));
console.log('::before', supportsSelector('::before'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment