X

LATEST #JQUERY NEWS

How to Use jQuery to Test CSS3 Browser Support


The tricky thing about using CSS properties (especially newer ones that have just been introduced) is that they aren’t always supported by every browser, particularly older versions of any browser, which can be a pain if you’re really striving for consistency in your projects. Luckily, there are a few ways around this issue of cross-browser compatibility. You could quickly test to be sure that your properties of choice are supported, and if they aren’t, you can choose to nix them, or conversely, you could choose to use the property, and write different code for the browsers that don’t support the property.

But how do you test to see if a CSS property will work on a browser or not? This easy to implement jQuery function (originally from Snipplr) can be used to quickly determine if a CSS3 property is supported by any browser. From there, you can make your own determination on how to handle the problem if your property proves to not be supported. See the snippet below.

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;
 };
})();
 
if ( supports('textShadow') ) {
 document.documentElement.className += ' textShadow';
}

It’s important to note that there is a small syntax quirk when using the snippet above. If the property you’re testing for is hyphenated in CSS (you can see above that the property being checked for is text-shadow), the hyphen needs to be removed and the property should be written in camelCase (where the hyphenated word is combined into one word, and the first letter is written in lowercase, and the first letters of the second, third, fourth, etc words are written in uppercase). So if you’re testing for a property like letter-spacing, you’d need to insert it into your code like this: letterSpacing.

The snippet above will test your CSS properties to see if they’re supported by most major browsers — including, Firefox, Safari, Chrome, Opera, and Internet Explorer.

Recently Published

article image
»

How to Use Google Analytics with jQuery Mobile

jQuery Mobile is an HTML5 based web-page framework that’s ...

article image
»

jQuery Snippets: Using jQuery to Check All Boxes

When you’ve given your users a ton of┬ácheckbox input fields ...

article image
»

Useful jQuery Code Snippets for Working with Images

Images are an essential part of any website and sometimes working ...

article image
»

How to Use jQuery to Check if Cookies are Enabled

A cookie (not the kind with chocolate chips) is a small piece of data ...

article image
»

A Beginner’s Guide to jQuery’s Selectors

We talk a lot about event methods on this site, but not so much about ...

article image
»

Add Keyboard Shortcut to Anchor Tag Using jQuery

Making a user friendly website can be quite a challenging task. There ...

Screen Shot 2017-04-23 at 10.15.41 AM
»

How to Use jQuery to Create a Sticky Navigation on Scroll

Creating “sticky” elements is something that can often be ...

article image
»

How to Use jQuery’s Keystroke Methods

jQuery has a few built-in keystroke methods that are all triggered by ...

article image
»

How to Change iFrame Styling Using jQuery

If you’ve ever had the displeasure of working with iFrames, ...