Creating Your First JQuery Plugin

Why Create a jQuery plugin

Sometimes its is useful to have a piece of functionality available throughout your code. Maybe you want a single function you can call on a jQuery selector that performs a specific action on such selector. Or maybe you wrote a utility function in one of your projects and now you want to be able to move it easily to other projects. In any case writing a plugin is your best option.

jQueryis great. It’s cross-browser, easy to learn, and helps you make very user-friendly interfaces. It also comes with a lot of useful plugins to do almost whatever you want.

But sometimes a lot is not enough, what if you can’t find just the right plugin to suit your needs? Or maybe there exist a plugin but its too large, and you just want part of it. The solution might be to roll up your selves and write your own stuff. After all it sound more complicated than it really is. This short tutorial will go through the process of writing a simple plugin, adding some options, and even perform a callback.

Setting Up

You can download the entire project from GitHub

For this tutorial, we will be creating a simple accordion plugin. Let’s create a js file and put it in the “js” directory of our website. It’s tradition to start all js plugin’s files with “jquery dot” followed by the actual plugin name, so we’ll call ours “jquery.simple-accordion.js”.

– Our plugin file inside the js directory inside our website directory

Now we need to include our plugin file along with the jQuery library to our main HTML page (index.html). It is best practice to include the jQuery library directly from the Google api servers, since they are distributed across the world instead of your single server location: Closer servers usually means faster response times for the visitor. Another advantage of having jQuery included from Google, is that when a visitor comes to your site they may already have the jQuery script in their local cache. Pre-cached content usually means faster load times for the visitor.

The jQuery Plugin Structure

jQuery is packed with all the necessary hooks to aid you in the development of our plugin. But is good to keep up the JavaScript good practices, so we must make sure everything is kept inside a local scope. Let’s start with the basic shell of a jQuery plugin:

(function($) {

$.fn.simpleAccordion = function() {

//TODO: code for simple accordion plugin

}

}(jQuery));

Let’s quickly go through what’s going on here. By including everything in the (function() {}) self-enclosed JavaScript pattern, we’re making sure that all the variables in our plugin will stay safely outside of the global namespace. We don’t to run into conflicts with any other javascript running in this page, so we must isolate our code as we did above.

The other thing you might notice is that we’re defining our plugin as if jQuery was in it’s “no-conflict” mode. Again, we’re seeking to avoid colliding with other JavaScript on the page, and thus we want to make sure that our plugin isn’t reliant on the default $, which could be used by another library.

Finally, $.fn is jQuery’s way of allowing you to define our plugin, which we’ve named simpleAccordion. With all of our pieces in place, let’s get cooking!

Adding Functionalities to Our Plugin

What is great about jQuery is that it lets you use any selector you want. However, you must keep in mind that our plug-in can be dealing with several different element types. Using the “this” keyword lets our plug-in apply the associated functions by accessing each element in a loop regardless of the element type.

Getting The HTML Part Ready – Structuring The Accordion

Since we are building an accordion we are going to need to HMLT structure for it, so here we go:

Heading 1

Cras dolor elit, porttitor ac diam bibendum, eleifend aliquam erat.

Heading 2

Donec blandit risus nec est tristique interdum.

Heading 3

Fusce sit amet arcu id justo malesuada faucibus.

Heading 4

Aliquam tincidunt lobortis sem at porttitor.

This is a simple accordion structure, defining four titles and their respective content areas. So the idea is that our plugin will turn this HTML code into a functional accordion, where an user can open and collapse different sections.

This is how it should look like for now:

Just to make it look pretty we should add some simple styles. So here we go:

.accordion {

width: 600px;

border: 1px solid #ccc;

border-bottom: none;

}

.accordion dt,.accordion dd {

border-bottom: 1px solid #ccc;

margin: 0px;

}

.accordion dt {

background: rgba(193, 221, 252, 0.24);

cursor: pointer;

padding: 8px 4px;

font-size: 14px;

font-weight: bold;

}

.accordion dd {

padding: 12px 8px;

}

Now our accordion structure should look like this:

The jQuery Part – Time To Make It Do Something!

To add functionality to our plugin we are going to edit the js file and add the following code:

(function($) {

$.fn.simpleAccordion = function(options) {

return this.each(function() {

var dts = $(this).children(‘dt’);

dts.click(accordionClick);

dts.each(reset);

});

function accordionClick() {

$(this).siblings(‘dt’).each(hide);

$(this).next().slideDown(‘fast’);

return false;

}

function hide() {

$(this).next().slideUp(‘fast’);

}

function reset() {

$(this).next().hide();

}

}

})(jQuery);

Quickly let’s go over what this code is doing. First, you need to understand that jQuery is a library that extends the JavaScript language. When creating any jQuery plug-in, you’re essentially extending the jQuery library, which is extending JavaScript. Truly understanding how your plug-in extends the jQuery library requires an understanding of the JavaScript prototype property. Although it is not used directly, the JavaScript prototype property is used behind the scenes through the jQuery property fn, which is a jQuery alias for the native JavaScript prototype property.

So in simpler terms, you extend your plugin from jQuery by using fn.”your-plugin-name”

In addition, we could add options to pass to our plugin. A jQuery plug-in can include defaults and options. Options are arguments that you could pass to your plug-in. Rather than sending several arguments, with a options object you can send multiple properties, which is the standard jQuery practice. When allowing options in your plug-in, it is a best practice to set default options using the defaults object. Like options, defaults are an object literal that should include the properties you are allowing to be passed to your plug-in.

Let’s quickly add some options to our plugin to show how simple this is. We are going to pass an option parameter to open the first section of our accordion when it is first loaded.

(function($) {

$.fn.simpleAccordion = function(options) {

var state = $.extend({}, {open: false}, options);

return this.each(function() {

var dts = $(this).children(‘dt’);

dts.click(accordionClick);

dts.each(reset);

if(state.open)

$(this).children(‘dt:first-child’).next().show();

});

function accordionClick() {

$(this).siblings(‘dt’).each(hide);

$(this).next().slideDown(‘fast’);

return false;

}

function hide() {

$(this).next().slideUp(‘fast’);

}

function reset() {

$(this).next().hide();

}

}

})(jQuery);

If you notice, we are using default value of open:false, so if no option is passed, the plugin will assume that you want your accordion closed when first loaded. When the plug-in receives options, you can rely on the $.extend method to do the actual overriding. What the $.extend method does is that it merges two or more objects.

That’s it!

So there you have it your first jQuery plugin, very simple right. I hope you enjoyed this tutorial, and find it useful. You can download the entire project from GitHub.

An Introduction to WordPress Plugins

Plugins are software components with different functions that can be incorporated into a WordPress website. They can expand functionality or include other features in your WordPress websites. Written in the PHP scripting language, WordPress plugins smoothly blend with WordPress. The saying “there’s a plugin for that” is commonly heard in the WordPress community. They help users by simplifying the process of adding features to their site even if they do not know a single code. Thousands of them can be downloaded at no cost at the official WordPress directory.

Site administrators can install or uninstall plugins from the administration area. They can download them, as well as install them manually with an FTP client.

Most of the plugins are free so it is important to keep in mind that they often do not include technical support. That is the reason why it is crucial to be careful in choosing the ones to install on your website. While a lot of plugins can practically do anything, some have better quality than others. To choose the best one, consider asking yourself a number of questions like when it was last updated. Does it work with the latest WordPress version? Do people receive answers whenever they ask support questions? What is its rating?

Some people say that WordPress plugins slow down your site. This is certainly not true. Only poor quality ones will slow it down.

Premium versus Free

A multitude of WordPress plugins can be downloaded for free at the WordPress.org Directory. Premium/paid WordPress plugins are available as well, and there are many different reasons why some users opt for a paid one:

Though the plugin directory comes with thousands of plugins that are free to download, paid plugins come with developers and staff that provides full-time support and make sure that security is maintained and plugins are compatible with the latest WordPress version, plus other themes and plugins.

Usually, plugins work well with the core of WordPress as well as other plugins; however, at times, a plugin’s code could block the way of another plugin, resulting to compatibility problems. It is a lot different with a paid plugin since there is a support staff that can provide assistance when something goes wrong.

Now, you may be wondering the types of things that plugins can achieve. Whatever you imagine, there is always a plugin for that purpose. You just need to go through the WordPress plugin directory to check all the possible ways that plugins can expand or improve your WordPress site.

IE9 Embraces IE Plugins Development As It Gets Rid of Vulnerabilities

Festive season is round the corner, and holidays are about to begin. Many web applications exist to help users travel in a convenient way. However, browsing through the holiday locations on several websites, and surfing for the hotels around the place users wish to visit, is a bit difficult. In such a scenario, IE plugin development or installation allow users to view quick details with just a click over the place users wish to visit. Different IE plugins offer different functionality and serve the users according to their varying needs.

Due to its obtrusive browsing facility, IE became an obsolete browser a few years back. However, it made a booming come back with its latest IE9 version, which support HTML 5 extensively. With frequent patch releases, IE9 intends to fill all the loop holes in its browser to make it competent in the market. Its new patch targets eight vulnerabilities; including the one in which hackers can remotely run the code on user’s PC. Microsoft called its patch very important as the patch also fixed holes in earlier versions of the browser such as IE6, IE7 and IE8. The fix also worked for vulnerabilities in Windows, Silverlight and the Dot Net Framework.

Besides, its recent HTML 5 support for mobile phones made it an important choice for smart phones, as there isn’t another standard of delivering video that works across all major browsers. Along with its HTML 5 support, IE9 also fixes the bug that messed up Exchange Management Console.

With all the fixes, IE9 proves to be the most efficient browser, and actually has the capacity to block more Malware than Chrome. Several reviewers gave a clean chit to IE9 encouraging the IE plugin development in the industry.

Latest Internet Explorer plugin development

Today, developers have, openly, taken up development of plug-ins for Internet Explorer. Several newly launched IE plugins include Flash Video Downloader. This downloader installs easily enough, but the discreet icon often goes unnoticed. Once users know the icon, they can simply click the icon to download a specific Flash video file. One of its disadvantages is a hassle of saving the file with.flv extension every time. Flash video Downloader is only for downloads and can’t be used to play the so called.flv files.

Another trivial yet important IE plugin development is a Tiff Viewer and Browser Plugin. The Tiff Viewer and Plug-in allow users to view, print and edit any TIFF image on the Internet. It works as a standalone image viewer on user’s machine and also works as a Fax cleaner.

Reputed companies engage in custom internet explorer Plugin development tailored to suit business or individual requirements. Browser extensions play a vital role in helping users to perform any activity with just a click. Browser Plugins add various functionality to the browser, which users cannot perform otherwise. Several companies offer their own extensions for the browser; however, they include only specified services and don’t let users customize it. Moreover, there is always a fear of data theft and malwares in an extension from an unauthorized source. Reputed companies offer the convenience of custom IE plugin development at a very minimal price.