3

I'm creating a layout in full css. However, some browser (such as IE6) do not support box-shadow (.. and -webkit-box-shadow or -moz-box-shadow). I would like to check if it's not supported and then add other styles.

How's this possible in jQuery?

Martti Laine

1

2 Answers 2

9
var check = document.createElement('div');

var shadow = !!(0 + check.style['MozBoxShadow']);
if(shadow)
   alert('moz-box-shadow available');

That is the doing-it-yourself way. Other reliable way is the modernizr library, which does feature detection for you.

http://www.modernizr.com/

No jQuery needed at all here.

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

6 Comments

@Stefan: So what exactly is the -1 for? :p
Not sure what issue @Stefan has with the DIY. The same check would probably be done for the WebkitBoxShadow and boxShadow, but it's not much of a stretch to figure that out.
It's like writing your own GCD function, when one exists. In case the check for boxshadow changes in the future, Modernizr could be updated to support this change. Any code you write yourself that exists in a library is bad code.
@Stefan: are you serious ? You'd suggest someone, who just needs to make one or two feature detections (which is about 30 bytes?) to use a lib that takes like 2k, 3k, 10k whatever? Don't get me wrong, I like modernizr, but for just one check is overkill. It's like using jQuery to select an element.
Yes, jQuery SHOULD be used to select elements. It's a one-time static hit, and if you point to a proper CDN you're likely to get a cache hit. Modern libraries are incredibly small when compressed, and this is an egregious copying of code. 5KB is NOT a significant amount of space. You're arguing to not use libraries, due to the potential "bloat". Copying code has a far more terrible impact on code longevity.
|
3

A neat function (pure JavaScript, no jQuery) to check which CSS features are supported by the browser is described in Quick Tip: Detect CSS3 Support in Browsers with JavaScript.

Function is as follows:

var supports = (function() {
   var div = document.createElement('div'),
      vendors = 'Khtml Ms O Moz Webkit'.split(' '),
      len = vendors.length;

   return function(prop) {
      if (prop in div.style) {
          return true;
      }

      prop = prop.replace(/^[a-z]/, function(val) {
         return val.toUpperCase();
      });

      while (len--) {
         if (vendors[len] + prop in div.style) {
            // browser supports box-shadow. Do what you need.
            // Or use a bang (!) to test if the browser doesn't.
            return true;
         } 
      }

      return false;
   };
})();

Usage is like this:

if (supports('boxShadow')) { 
   // Do whatever
}

It worked like a charm for me! :-)

Comments

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.