Plugins?
We all love using plugins for jQuery, it cuts development time, and in most cases if you can think of an idea, someone has made a plugin for it.
Occasionally though, you need to make your own, or you simply want to make your own!
One of the most useful plugins is the easing plugin. It allows you to add advanced easing to any of yor animations.
The Problem
Whilst writing a jQuery plugin recently, I found that I needed to see if the easing plugin was available.
I couldn't use the typeof function like you can in most cases:
- // plugin detection code:
- if(typeof(jQuery.pluginFunction)) {
- }
This is because jQuery.easing is a built in function to handle the two default jQuery easing types.
The Solution
So I decided to check for the presence of the default easing string. If that is not set, it is safe to assume the easing plugin is not available.
- if( $.easing.def ) {
- alert('Hooray, we have the easing Plugin!');
- }else {
- alert('Boo! No Plugin');
- }
Practical Application
The above code is not too useful on its own, so allow me to explain why I wanted to do this.
I didn't want someone passing in a value for easing from the easing plugin if they didn't actually have the plugin.
If that happens, it would break the code, so I needed to handle this before any damage was done. Here's how I did it:
- //set up a default value for easing
- settings.defEasing = 'swing';
- // check for the existence of the easing plugin
- if( $.easing.def) {
- // ok, we can set some value to test against later
- $(this).data('easing', {plugin: true});
- }else {
- // this is where problems could arise...
- $(this).data('easing', {plugin: false});
- // check to see if the value passed in for easing is one of the default values
- if(settings.easing != 'swing' && settings.easing != 'linear') {
- // set the value to one of the 'safe', default values.
- settings.easing = settings.defEasing;
- }
- }
If the easing property is not one of the built-in jQuery easing types, and the easing plugin does not exist, the code defaults to one of the built-in properties to stop the code from failing.
If you want to know more about Javascript, xhtml and web development form a guy who really knows his stuff, then check out Jim Highson's Blog.
Post new comment