GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||
GeezerVison is showing learn.jquery.com — How to Create a Basic Plugin | jQuery Learning Center Sometimes you want to make a piece of functionality available throughout your code. For example, perhaps you want a single method you can call on a jQuery selection that performs a series of operations on the selection. In this case, you may want to write a plugin. link How jQuery Works 101: jQuery Object MethodsBefore we write our own plugins, we must first understand a little about how jQuery works. Take a look at this code:
This is some pretty basic jQuery code, but do you know what's happening behind the scenes? Whenever you use the link Basic Plugin AuthoringLet's say we want to create a plugin that makes text within a set of retrieved elements green. All we have to do is add a function called
Notice that to use link ChainingThis works, but there are a couple of things we need to do for our plugin to survive in the real world. One of jQuery's features is chaining, when you link five or six actions onto one selector. This is accomplished by having all jQuery object methods return the original jQuery object again (there are a few exceptions:
link Protecting the $ Alias and Adding ScopeThe
In addition, the primary purpose of an Immediately Invoked Function is to allow us to have our own private variables. Pretend we want a different color green, and we want to store it in a variable.
link Minimizing Plugin FootprintIt's good practice when writing plugins to only take up one slot within
It would be much better to have one slot, and use parameters to control what action that one slot performs.
link Using the |
|
1
2
3
4
5
6
7
|
|
Notice that we return the results of .each() instead of returning this. Since .each() is already chainable, it returns this, which we then return. This is a better way to maintain chainability than what we've been doing so far.
As your plugins get more and more complex, it's a good idea to make your plugin customizable by accepting options. The easiest way to do this, especially if there are lots of options, is with an object literal. Let's change our greenify plugin to accept some options.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
|
Example usage:
|
1
2
3
|
|
The default value for color of #556b2f gets overridden by $.extend() to be orange.
Here's an example of a small plugin using some of the techniques we've discussed:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
|
This handy plugin goes through all anchors in the collection and appends the href attribute in parentheses.
|
1
2
3
4
5
|
|
Our plugin can be optimized though:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
|
We're using the .append() method's capability to accept a callback, and the return value of that callback will determine what is appended to each element in the collection. Notice also that we're not using the .attr() method to retrieve the href attribute, because the native DOM API gives us easy access with the aptly named href property.