3 * jQuery Once Plugin v1.2
4 * http://plugins.jquery.com/project/once
6 * Dual licensed under the MIT and GPL licenses:
7 * http://www.opensource.org/licenses/mit-license.php
8 * http://www.gnu.org/licenses/gpl.html
12 var cache
= {}, uuid
= 0;
15 * Filters elements by whether they have not yet been processed.
18 * (Optional) If this is a string, then it will be used as the CSS class
19 * name that is applied to the elements for determining whether it has
20 * already been processed. The elements will get a class in the form of
23 * If the id parameter is a function, it will be passed off to the fn
24 * parameter and the id will become a unique identifier, represented as a
27 * When the id is neither a string or a function, it becomes a unique
28 * identifier, depicted as a number. The element's class will then be
29 * represented in the form of "jquery-once-#-processed".
31 * Take note that the id must be valid for usage as an element's class name.
33 * (Optional) If given, this function will be called for each element that
34 * has not yet been processed. The function's return value follows the same
35 * logic as $.each(). Returning true will continue to the next matched
36 * element in the set, while returning false will entirely break the
39 $.fn
.once = function (id
, fn
) {
40 if (typeof id
!= 'string') {
41 // Generate a numeric ID if the id passed can't be used as a CSS class.
45 // When the fn parameter is not passed, we interpret it from the id.
49 id
= 'jquery-once-' + cache
[id
];
51 // Remove elements from the set that have already been processed.
52 var name
= id
+ '-processed';
53 var elements
= this.not('.' + name
).addClass(name
);
55 return $.isFunction(fn
) ? elements
.each(fn
) : elements
;
59 * Filters elements that have been processed once already.
62 * A required string representing the name of the class which should be used
63 * when filtering the elements. This only filters elements that have already
64 * been processed by the once function. The id should be the same id that
65 * was originally passed to the once() function.
67 * (Optional) If given, this function will be called for each element that
68 * has not yet been processed. The function's return value follows the same
69 * logic as $.each(). Returning true will continue to the next matched
70 * element in the set, while returning false will entirely break the
73 $.fn
.removeOnce = function (id
, fn
) {
74 var name
= id
+ '-processed';
75 var elements
= this.filter('.' + name
).removeClass(name
);
77 return $.isFunction(fn
) ? elements
.each(fn
) : elements
;