Cover by Cody Lindley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

O'Reilly logo

3.8. Adding Functionality with Plugins

Problem

The jQuery library is a small, slick, powerful JavaScript library, but it doesn’t come preloaded with every piece of functionality that you may need.

Solution

jQuery was built with extensibility in mind. If the core jQuery library can’t do what you want, chances are a jQuery plugin author has written a plugin that will handle your need, probably in as little as one line of code.

To include a plugin on your page, all you need to do is download the plugin .js file, include the jQuery library on the page, then immediately after, include your plugin on the page. Then, in either another .js file or in a script block on the page, you’ll typically need to call the plugin and provide any options that may be required.

Here is an example using the jQuery cycle plugin developed by Mike Alsup:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title>Chapter 3 - Recipe 8 - Adding Functionality with Plugins</title> <style type="text/css"> .pics { height: 232px; width: 232px; padding: 0; margin: 0; } .pics img { padding: 15px; border: 1px solid #ccc; background-color: #eee; width: 200px; height: 200px; top: 0; left: 0 } </style> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> <!--Now include your plugin ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required