Chapter 6. Function plugins

 

This chapter covers

  • Defining a function plugin
  • Localizing content via a function plugin
  • Accessing cookies via a function plugin

The collection plugins from the previous chapter worked by operating on a set of elements retrieved from the page via a selection and/or traversal process, but you can also create plugins that don’t apply to such collections but provide utility functions within the jQuery framework. These are function plugins.

Examples of this type of plugin include the Debug plugin (http://jquery.glyphix.com/), which provides logging output for debugging purposes, and the Cookie plugin, which provides interactions with the cookies for a website (covered in detail in section 6.2). As with the earlier plugins, you’re only limited by your imagination in what you can create.

Because function plugins don’t work with sets of elements from the page and often work without any UI component, they’re considerably simpler to implement. Although you could define these functions as standalone JavaScript functions, there are benefits to creating them within the jQuery namespace. Doing so reduces the clutter in the global namespace and reduces the risk of a name clash. Often they make use of jQuery itself, and their inclusion there offers a consistent approach when using jQuery. They also aim to make the provided abilities easier to use and to hide any cross-browser differences: key principles behind jQuery itself.

6.1. Defining your plugin

6.2. jQuery Cookie plugin

6.3. Summary