Created
June 17, 2010 08:27
-
-
Save paulirish/441842 to your computer and use it in GitHub Desktop.
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
// selectorSupported lovingly lifted from the mad italian genius, diego perini | |
// http://javascript.nwbox.com/CSSSupport/ | |
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); | |
}; | |
Modernizr.addTest('targetselector',function(){ | |
return selectorSupported(':target'); | |
}) | |
can't**
Awesome function, thank you very much.
Also thanks to javier, I wanted to test exaclty what he found, :nth-child(n)
.
This doesn't work on IE9:
>> selectorSupported('div ~ div')
false
>> selectorSupported('div + div')
false
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm sure this works great - but I can implement... an example would be very useful...