Skip to content

Instantly share code, notes, and snippets.

@cferdinandi
Last active December 21, 2015 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cferdinandi/6270183 to your computer and use it in GitHub Desktop.
Save cferdinandi/6270183 to your computer and use it in GitHub Desktop.
A simple feature test to detect pseudo selector support. Via Paul Irish and Diego Perini: https://gist.github.com/paulirish/441842
/**
* Test for pseudo-selector support
* @author Paul Irish
* @link https://gist.github.com/paulirish/441842
* @param {String} selector Pseudo-selector to test
* @return {Boolean}
*/
function selectorSupported(selector){
var support, link, sheet, doc = document,
root = doc.documentElement,
head = root.getElementsByTagName('head')[0],
impl = doc.implementation || {
hasFeature: function() {
return false;
}
},
link = doc.createElement("style");
link.type = 'text/css';
(head || root).insertBefore(link, (head || root).firstChild);
sheet = link.sheet || link.styleSheet;
if (!(sheet && selector)) return false;
support = impl.hasFeature('CSS2', '') ?
function(selector) {
try {
sheet.insertRule(selector + '{ }', 0);
sheet.deleteRule(sheet.cssRules.length - 1);
} catch (e) {
return false;
}
return true;
} : function(selector) {
sheet.cssText = selector + ' { }';
return sheet.cssText.length !== 0 && !(/unknown/i).test(sheet.cssText) && sheet.cssText.indexOf(selector) === 0;
};
return support(selector);
};
// Example: If ':before' supported, add '.pseudo-selectors' class to the <html> element
// if (selectorSupported(':before')) {
// document.documentElement.className += 'pseudo-selectors';
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment