4

How to check a certain CSS capability in a browser using JavaScript without detecting its vendor, userAgent, or appName?

3
  • 1
    Which one did you have in mind? Commented Jun 8, 2011 at 8:55
  • 1
    @Tomgrohl, Thanks in advance, no particular browser. I wanted to test a CSS capability instead of asking for the browsers name, just like the proper way in testing a JavaScript capability. Commented Jun 8, 2011 at 8:58
  • Gotcha, added an answer that may be of some use. Commented Jun 8, 2011 at 8:59

4 Answers 4

3

I've used something like this when creating a cssHook in jQuery:

Testing for a CSS Property:

var div = document.createElement("div"),
    divStyle = div.style;

    $.support.boxSizing =
    divStyle.MozBoxSizing === ''? 'MozBoxSizing' : 
    (divStyle.WebkitBoxSizing === ''? 'WebkitBoxSizing' : 
    (divStyle.MsBoxSizing === ''? 'MsBoxSizing' :
    (divStyle.boxSizing === ''? 'boxSizing' : false)));

    div = divStyle = null; //release memory

Testing for a CSS Property Value:

var div = document.createElement( "div" ),
    css = "background-image:gradient(linear,left top,right bottom, from(#9f9), to(white));background-image:-webkit-gradient(linear,left top,right bottom,from(#9f9),to(white));background-image:-moz-gradient(linear,left top,right bottom,from(#9f9),to(white));background-image:-o-gradient(linear,left top,right bottom,from(#9f9),to(white));background-image:-ms-gradient(linear,left top,right bottom,from(#9f9),to(white));background-image:-khtml-gradient(linear,left top,right bottom,from(#9f9),to(white));background-image:linear-gradient(left top,#9f9, white);background-image:-webkit-linear-gradient(left top,#9f9, white);background-image:-moz-linear-gradient(left top,#9f9, white);background-image:-o-linear-gradient(left top,#9f9, white);background-image:-ms-linear-gradient(left top,#9f9, white);background-image:-khtml-linear-gradient(left top,#9f9, white);";    

    div.style.cssText = css;

$.support.linearGradient =
    div.style.backgroundImage.indexOf( "-moz-linear-gradient" )  > -1 ? '-moz-linear-gradient' :
    (div.style.backgroundImage.indexOf( "-webkit-gradient" )  > -1 ? '-webkit-gradient' :
    (div.style.backgroundImage.indexOf( "gradient" )  > -1 ? 'gradient' : false));

    div= null; //release memory
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic example and you don't even need jQuery to use this.
2

See jQuery.support

Comments

1

The problem with this question is that the technique is different for each property.

The general idea is to use JavaScript to attempt to use the property, and then to test for some defined behaviour of the property.

See the source code to Modernizr, a feature detection library:

http://www.modernizr.com/ - http://www.modernizr.com/downloads/modernizr-2.0.3.js

For instance:

// http://css-tricks.com/rgba-browser-support/
tests['rgba'] = function() {
    // Set an rgba() color and check the returned value

    setCss('background-color:rgba(150,255,150,.5)');

    return contains(mStyle.backgroundColor, 'rgba');
};

In a browser that does not support rgba, the returned value would not contain rgba.

Also, as suggested by @DanielB, look at jQuery.support for more inspiration. Here's the source:

https://github.com/jquery/jquery/blob/master/src/support.js

Comments

1

You could use a library like modernizr which can tell you which features are available for the current visitor.

1 Comment

Took a bit of crack at this here: fractured-state.com/2011/07/…

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.