5

Is it possible to detect CSS support by using Javascript?

For example, is it possible to detect if the browser supports attribute selectors like this?

input[type='text'] { }
3
  • How would this be useful? You have to run JavaScript to check, so you might as well just run JavaScript to add support for it. Commented Aug 18, 2011 at 15:44
  • @thirtydot, yeah I know what you're saying, but I guess that the less work the browser has to do in Javascript, the better. If the CSS engine can handle it, why waste precious cycles? ;) Commented Aug 18, 2011 at 15:53
  • 1
    Because the CSS engine (in IE6-8) can't handle it. Selectivizr immediately quits if the browser is not IE6-8 (the only common browsers that don't support this stuff). Otherwise, it runs and adds support for a bunch of lovely selectors. Commented Aug 18, 2011 at 15:58

3 Answers 3

5

Modernizr is designed to detect browser features and may well be able to help in this instance. http://www.modernizr.com/

Sign up to request clarification or add additional context in comments.

2 Comments

Only really detects new stuff... [attr] selectors are everywhere (except IE6)
Modernizr is for HTML4-->5 transitional coding, not a generic toolkit for checking legacy browser capabilities. Bad (and imo, incorrect) use for this library. Use it to make sure that you can work with a "canvas" and other new elements regardless of HTML5 support, not to verify that legacy capabilities still exist in a browser.
3

This is a bit speculative as I haven't tested it out, but I believe it would be possible via JS to add a style element followed by an element that it has an effect on, and then test the values:

Speculative untested code, may or may not work (jQuery used for brevity):

$('<style type="text/css" id="foo">input[type="text"]{ width: 10px; }</style>').appendTo('head');
$('<input type="text" id="bar">').appendTo('body');
if ($('#bar').width() == 10)
{
  //attr selector supported
}
$('#foo, #bar').remove();

2 Comments

+1 for the approach, as its probably the only way you will be able to detect it.
Agree with @gnarf on this one - short of a try catch around querySelector or some other hack...
2
document.querySelectorAll("input[type='text']")

But that fails for older browsers, naturally.

Other than that, you could just use the style property to check if a certain CSS property has been applied or not.

input[type='text'] {
  background-repeat: no-repeat; /* or any other irrelevant, non-default value */
}

and

if (myInputElem.style.backgroundRepeat == "no-repeat") {
  // selector is supported
}

2 Comments

Anything that supports qSA supports [attr] selectors
@gnarf: That's probably true. Checking for the existence of that function might be a hint as well, even though that's not exactly what has been asked for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.