Testing Plugins for HTML Features

By April 19, 2014Blog

When we’re building our plugins we’ll often want to test if certain features are supported by a particular browser. There are some scripts out there that will help you with this, however it’s probably not best to make a small simple plugin dependent on such libraries. It’s better we just check for features that are specific to our jQuery plugin.

The great thing with jQuery is that there is already a place we can do this. If you display the contents of $.support you will see a list of internal tests jQuery is already doing for it’s own purposes. We can just piggy back off of this and add our own checks.

For instance say we want to check for canvas support.

$.support.canvas = (document.createElement('canvas')).getContext;

This simple line above will return true or false allowing us to simply use $.support.canvas instead of the messier createElement piece of code. There are much more check we can do ranging from support for specific elements to camera and audio support. A great resource to check is the HTML5test and modernizer website which provide some libraries and code samples you can use for your own projects.