├── .editorconfig ├── .github └── workflows │ └── tests.yml ├── .gitignore ├── API.md ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── bower.json ├── component.json ├── composer.json ├── example └── index.html ├── jquery.once.d.ts ├── jquery.once.js ├── jquery.once.min.js ├── jquery.once.min.js.map ├── once.jquery.json ├── package.json └── test └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig 2 | # http://editorconfig.org 3 | root = true 4 | 5 | [*] 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | indent_style = space 11 | indent_size = 2 12 | -------------------------------------------------------------------------------- /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: tests 2 | 3 | on: 4 | pull_request: 5 | paths-ignore: 6 | - '**.md' 7 | push: 8 | branches: 9 | - master 10 | paths-ignore: 11 | - '**.md' 12 | 13 | jobs: 14 | testing: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v2 18 | - uses: actions/setup-node@v1 19 | with: 20 | node-version: '18' 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Gitignore 2 | # https://git-scm.com/docs/gitignore 3 | 4 | # Log files 5 | **/*.log 6 | 7 | # Vendor packages 8 | /components 9 | /bower_components 10 | /node_modules 11 | /vendor 12 | /composer.lock 13 | /package-lock.json 14 | 15 | # Code coverage with Istanbul 16 | /coverage 17 | /.nyc_output 18 | -------------------------------------------------------------------------------- /API.md: -------------------------------------------------------------------------------- 1 | ## Functions 2 | 3 |
jQuery
Filter elements that have yet to be processed by the given data ID.
6 |jQuery
Removes the once data from elements, based on the given ID.
9 |jQuery
Filters elements that have already been processed once.
12 |jQuery
18 | Filter elements that have yet to be processed by the given data ID.
19 |
20 | **Kind**: global function
21 | **Returns**: jQuery
- jQuery collection of elements that have now run once by
22 | the given ID.
23 | **this**: jQuery
24 | **Access**: public
25 | **See**
26 |
27 | - removeOnce
28 | - findOnce
29 |
30 |
31 | | Param | Type | Default | Description |
32 | | --- | --- | --- | --- |
33 | | [id] | string
| "once"
| The data ID used to determine whether the given elements have already been processed or not. Defaults to `'once'`. |
34 |
35 | **Example**
36 | ``` javascript
37 | // The following will change the color of each paragraph to red, just once
38 | // for the 'changecolor' key.
39 | $('p').once('changecolor').css('color', 'red');
40 |
41 | // .once() will return a set of elements that yet to have the once ID
42 | // associated with them. You can return to the original collection set by
43 | // using .end().
44 | $('p')
45 | .once('changecolorblue')
46 | .css('color', 'blue')
47 | .end()
48 | .css('color', 'red');
49 |
50 | // To execute a function on the once set, you can use jQuery's each().
51 | $('div.calendar').once().each(function () {
52 | // Since there is no once ID provided here, the key will be 'once'.
53 | });
54 | ```
55 |
56 |
57 | ## removeOnce([id]) ⇒ jQuery
58 | Removes the once data from elements, based on the given ID.
59 |
60 | **Kind**: global function
61 | **Returns**: jQuery
- jQuery collection of elements that were acted upon to remove their
62 | once data.
63 | **this**: jQuery
64 | **Access**: public
65 | **See**: once
66 |
67 | | Param | Type | Default | Description |
68 | | --- | --- | --- | --- |
69 | | [id] | string
| "once"
| A string representing the name of the data ID which should be used when filtering the elements. This only filters elements that have already been processed by the once function. The ID should be the same ID that was originally passed to the once() function. Defaults to `'once'`. |
70 |
71 | **Example**
72 | ``` javascript
73 | // Remove once data with the 'changecolor' ID. The result set is the
74 | // elements that had their once data removed.
75 | $('p').removeOnce('changecolor').css('color', '');
76 |
77 | // Any jQuery function can be performed on the result set.
78 | $('div.calendar').removeOnce().each(function () {
79 | // Remove the calendar behavior.
80 | });
81 | ```
82 |
83 |
84 | ## findOnce([id]) ⇒ jQuery
85 | Filters elements that have already been processed once.
86 |
87 | **Kind**: global function
88 | **Returns**: jQuery
- jQuery collection of elements that have been run once.
89 | **this**: jQuery
90 | **Access**: public
91 | **See**: once
92 |
93 | | Param | Type | Default | Description |
94 | | --- | --- | --- | --- |
95 | | [id] | string
| "once"
| A string representing the name of the data id which should be used when filtering the elements. This only filters elements that have already been processed by the once function. The id should be the same id that was originally passed to the once() function. Defaults to 'once'. |
96 |
97 | **Example**
98 | ``` javascript
99 | // Find all elements that have been changecolor'ed once.
100 | $('p').findOnce('changecolor').each(function () {
101 | // This function is called for all elements that has already once'd.
102 | });
103 |
104 | // Find all elements that have been acted on with the default 'once' key.
105 | $('p').findOnce().each(function () {
106 | // This function is called for all elements that have been acted on with
107 | // a 'once' action.
108 | });
109 | ```
110 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 | All notable changes to this project will be documented in this file.
3 | This project adheres to [Semantic Versioning](http://semver.org/).
4 |
5 | ## [2.3.0] - January 4th, 2024
6 | ### Changed
7 | - Switched from `var` to `let` and `const` for variable scope
8 | - Removed `use strict` directives
9 | - Updated developer dependencies
10 | - Minor updates to developer documentation
11 | - Switched to GitHub Actions for testing
12 |
13 | ## [2.2.3] - May 14th, 2019
14 | ### Fixed
15 | - Fix CommonJS factory when export element is present
16 | - By [Sam152](https://github.com/Sam152) from [#91](https://github.com/RobLoach/jquery-once/issues/91)
17 |
18 | ## [2.2.2] - May 8th, 2019
19 | ### Changed
20 | - Updated developer dependencies and documenation
21 |
22 | ## [2.2.1] - March 29th, 2018
23 | ### Changed
24 | - Updated dependencies
25 |
26 | ## [2.2.0] - June 15th, 2017
27 | ### Fixed
28 | - Fixed throwing `Error` to `TypeError` when passing an incorrect `string` parameter
29 |
30 | ### Changed
31 | - Updated dependencies
32 | - Added [TypeScript](http://www.typescriptlang.org/) definition
33 | - By [olavorn](https://github.com/olavorn)
34 |
35 | ## [2.1.2] - June 11th, 2016
36 | ### Changed
37 | - Updated development dependencies
38 | - Updated documentation
39 | - Switched from [`semistandard`](http://npm.im/semistandard) to [`xo`](http://npm.im/xo) for coding standards
40 | - Tested in [jQuery 3.0](https://blog.jquery.com/2016/06/09/jquery-3-0-final-released/)
41 |
42 | ## [2.1.1] - August 31st, 2015
43 | ### Fixed
44 | - Corrected version information in the source
45 |
46 | ## [2.1.0] - August 31st, 2015
47 | ### Changed
48 | - Switched to [Keep a CHANGELOG](http://keepachangelog.com) in CHANGELOG.md
49 | - Moved to [JavaScript Semi-Standard Coding Style](http://npm.im/semistandard)
50 | - Updated development dependencies
51 |
52 | ## [2.0.2] - June 5th, 2015
53 | ### Added
54 | - Added code coverage
55 | - Added [jsDelivr CDN](http://www.jsdelivr.com/#!jquery-once) automated support
56 | - Added [cdnjs](https://github.com/cdnjs/cdnjs) automated support
57 |
58 | ## [2.0.1] - May 5th, 2015
59 | ### Changed
60 | - Updated development dependencies
61 | - Updated documentation
62 |
63 | ## [2.0.0] - January 20th, 2015
64 | ### Fixed
65 | - Fixed type checking of the `id` parameter of `.once()` as optional
66 | - From [@theodoreb](http://github.com/theodoreb)
67 | - Fixed inline code documentation
68 | - From [@yched](http://github.com/yched)
69 |
70 | ### Added
71 | - Added performance improvement through [`.data()`](http://api.jquery.com/data/)
72 | use rather than class attributes
73 | - Added `findOnce()` function to allow filtering once'd elements
74 | - Added automated testing through [Mocha](http://mochajs.org)
75 | - Added [jsdoc-to-markdown](https://github.com/75lb/jsdoc-to-markdown) to
76 | automatically build API documentation
77 |
78 | ### Changed
79 | - Switched to [ESLint](http://eslint.org) for code linting
80 | - Removed unneeded cache variable
81 | - Removed function callback in order to promote jQuery chaining standards
82 |
83 | ## [1.2.6] - August 31, 2013
84 | ### Changed
85 | - Fixed Bower
86 | - Updated documentation
87 |
88 | ## [1.2.4] - June 13, 2013
89 | ### Added
90 | - Added jquery.once.min.js to the file meta data
91 | - Added removeOnce() test
92 |
93 | ### Changed
94 | - Don't limit jQuery.once usage to jQuery 1.8.
95 | - Updated documentation
96 |
97 | ## [1.2.3] - June 13, 2013
98 | ### Added
99 | - Added tests
100 | - Fixed documentation
101 |
102 | ## [1.2.1] - May 18, 2013
103 | ### Added
104 | - Added UMD support
105 | - Added Bower support
106 |
107 | ## [1.2.0] - April 5, 2013
108 | ### Added
109 | - Added jQuery Once
110 |
111 | [unreleased]: https://github.com/RobLoach/jquery-once/compare/2.2.3...HEAD
112 | [2.2.3]: https://github.com/RobLoach/jquery-once/compare/2.2.2...2.2.3
113 | [2.2.2]: https://github.com/RobLoach/jquery-once/compare/2.2.1...2.2.2
114 | [2.2.1]: https://github.com/RobLoach/jquery-once/compare/2.2.0...2.2.1
115 | [2.2.0]: https://github.com/RobLoach/jquery-once/compare/2.1.2...2.2.0
116 | [2.1.2]: https://github.com/RobLoach/jquery-once/compare/2.1.1...2.1.2
117 | [2.1.1]: https://github.com/RobLoach/jquery-once/compare/2.1.0...2.1.1
118 | [2.1.0]: https://github.com/RobLoach/jquery-once/compare/2.0.2...2.1.0
119 | [2.0.2]: https://github.com/RobLoach/jquery-once/compare/2.0.1...2.0.2
120 | [2.0.1]: https://github.com/RobLoach/jquery-once/compare/2.0.0...2.0.1
121 | [2.0.0]: https://github.com/RobLoach/jquery-once/compare/1.2.6...2.0.0
122 | [1.2.6]: https://github.com/RobLoach/jquery-once/compare/1.2.4...1.2.6
123 | [1.2.4]: https://github.com/RobLoach/jquery-once/compare/1.2.3...1.2.4
124 | [1.2.3]: https://github.com/RobLoach/jquery-once/compare/1.2.1...1.2.3
125 | [1.2.1]: https://github.com/RobLoach/jquery-once/compare/1.2.0...1.2.1
126 | [1.2.0]: https://github.com/RobLoach/jquery-once/compare/7db530a0bd48f249c5f0df4fab02e93444623889...1.2.0
127 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | # License
2 |
3 | Copyright © 2016-2024 [Rob Loach](http://github.com/RobLoach)
4 |
5 | ## GPL-2.0
6 |
7 | > [GPL-2.0](http://opensource.org/licenses/gpl-2.0.php)
8 |
9 | ## The MIT License
10 |
11 | > Permission is hereby granted, free of charge, to any person obtaining a copy
12 | > of this software and associated documentation files (the "Software"), to deal
13 | > in the Software without restriction, including without limitation the rights
14 | > to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 | > copies of the Software, and to permit persons to whom the Software is
16 | > furnished to do so, subject to the following conditions:
17 | >
18 | > The above copyright notice and this permission notice shall be included in
19 | > all copies or substantial portions of the Software.
20 | >
21 | > THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 | > IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 | > FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 | > AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 | > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 | > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 | > SOFTWARE.
28 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # jQuery Once [](https://npmjs.org/package/jquery-once "View this project on NPM")
2 |
3 | 
4 | [](https://npmjs.org/package/jquery-once "View this project on NPM")
5 |
6 | > Act on [jQuery](http://jquery.com) elements only once.
7 |
8 | Filters out all elements that had the same filter applied on them before. It
9 | can be used to ensure that a function is only applied once to an element.
10 |
11 | ## Install
12 |
13 | Method | Installation
14 | ------ | ------------
15 | [npm](http://npmjs.com/package/jquery-once) | `npm install jquery-once --save`
16 | [Composer](https://packagist.org/packages/robloach/jquery-once) | `composer require robloach/jquery-once`
17 | [Bower](http://bower.io/search/?q=jquery-once) | `bower install jquery-once`
18 | [Component](https://github.com/componentjs/component) | `component install RobLoach/jquery-once`
19 | [jsDelivr](http://www.jsdelivr.com/#!jquery.once) | `//cdn.jsdelivr.net/npm/jquery-once@2.3.0/jquery.once.min.js`
20 | [cdnjs](https://cdnjs.com/libraries/jquery-once) | `//cdnjs.cloudflare.com/ajax/libs/jquery-once/2.3.0/jquery.once.js`
21 |
22 | ## Usage
23 |
24 | [See the API documentation for more information on how to use jQuery Once.](https://github.com/RobLoach/jquery-once/blob/master/API.md#readme)
25 |
26 | ``` javascript
27 | // The following will change the color of each paragraph to red, just once
28 | // for the "changecolor" key.
29 | $('p').once('changecolor').css('color', 'red');
30 |
31 | // .once() will return a set of elements that yet to have the once ID
32 | // associated with them. You can return to the original collection set by
33 | // using .end().
34 | $('p')
35 | .once("changecolorblue")
36 | .css("color", "blue")
37 | .end()
38 | .css("color", "red");
39 |
40 | // To execute a function on the once set, you can use jQuery's each().
41 | $('div.calendar').once().each(function() {
42 | // Since there is no once ID provided here, the key will be "once".
43 | });
44 | ```
45 |
46 | ## Development
47 |
48 | 1. Ensure you are using [node](http://nodejs.org) >= 4:
49 | ```
50 | node --version
51 | ```
52 |
53 | 2. Install dependencies through [npm](http://npmjs.org):
54 | ```
55 | npm install
56 | ```
57 |
58 | 3. Check coding style standard, and automated testing:
59 | ```
60 | npm test
61 | ```
62 |
63 | 4. Build `jquery.once.min.js` with:
64 | ```
65 | npm run build
66 | ```
67 |
68 | 5. Update API documentation:
69 | ```
70 | npm run docs
71 | ```
72 |
73 | 6. Tag and publish the new versions to [npm](http://npmjs.com) with [Semantic
74 | Versioning](http://semver.org/):
75 | ```
76 | git add -A
77 | git commit -m "2.3.0"
78 | git tag 2.3.0
79 | git push origin 2.3.0
80 | npm publish
81 | ```
82 |
83 | ## Change Log
84 |
85 | [Discover the change history by heading on over to the `CHANGELOG.md` file.](CHANGELOG.md)
86 |
87 | ## License
88 |
89 | Dual licensed under:
90 |
91 | - [GPL-2.0](http://opensource.org/licenses/gpl-2.0.php)
92 | - the incredibly [permissive](http://en.wikipedia.org/wiki/Permissive_free_software_licence) [MIT license](http://opensource.org/licenses/MIT)
93 |
94 | Copyright © [Rob Loach](http://github.com/RobLoach)
95 |
--------------------------------------------------------------------------------
/bower.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "jquery-once",
3 | "homepage": "http://github.com/robloach/jquery-once",
4 | "description": "Act on jQuery elements only once.",
5 | "license": "GPL-2.0",
6 | "main": "jquery.once.js",
7 | "ignore": [
8 | "*.json",
9 | "test",
10 | "example",
11 | ".travis.yml",
12 | ".gitignore",
13 | "Gruntfile.js"
14 | ],
15 | "keywords": [
16 | "jquery",
17 | "jquery-plugin"
18 | ],
19 | "dependencies": {
20 | "jquery": "*"
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/component.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "jquery-once",
3 | "repo": "robloach/jquery-once",
4 | "homepage": "http://github.com/robloach/jquery-once",
5 | "description": "Act on jQuery elements only once.",
6 | "license": "GPL-2.0",
7 | "version": "2.3.0",
8 | "keywords": [
9 | "jquery",
10 | "jquery-plugin"
11 | ],
12 | "dependencies": {
13 | "components/jquery": "*"
14 | },
15 | "main": "jquery.once.js",
16 | "scripts": [
17 | "jquery.once.js"
18 | ],
19 | "files": [
20 | "jquery.once.js"
21 | ],
22 | "demo": "http://github.com/robloach/jquery-once"
23 | }
24 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "title": "jQuery Once",
3 | "name": "robloach/jquery-once",
4 | "description": "Act on jQuery elements only once.",
5 | "homepage": "http://github.com/robloach/jquery-once",
6 | "type": "component",
7 | "license": [
8 | "MIT",
9 | "GPL-2.0"
10 | ],
11 | "require": {
12 | "components/jquery": "*"
13 | },
14 | "extra": {
15 | "component": {
16 | "scripts": [
17 | "jquery.once.js"
18 | ],
19 | "files": [
20 | "jquery.once.min.js",
21 | "jquery.once.min.js.map"
22 | ],
23 | "shim": {
24 | "deps": ["jquery"]
25 | }
26 | }
27 | },
28 | "archive": {
29 | "exclude": [
30 | ".*",
31 | "*",
32 | "!jquery.once*",
33 | "!README.md",
34 | "!LICENSE.md",
35 | "!composer.json"
36 | ]
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/example/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | This is some text, which should stay green.
10 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /jquery.once.d.ts: -------------------------------------------------------------------------------- 1 | /*! 2 | * jQuery Once v2.3.0 Typescript Definition - http://github.com/robloach/jquery-once 3 | * @license MIT, GPL-2.0 4 | * http://opensource.org/licenses/MIT 5 | * http://opensource.org/licenses/GPL-2.0 6 | * Author: Olavo Rocha Neto (github: olavorn) 7 | */ 8 | 9 | ///This is the Test.
'; 36 | }); 37 | 38 | it('should require ID to be a string', () => { 39 | // Expect it to throw an error. 40 | assert.throws(() => { 41 | $('span').once(() => { 42 | // Nothing. 43 | }); 44 | }); 45 | }); 46 | 47 | it('properly executes .once("test2")', () => { 48 | // Create one once('test2') call. 49 | $('span').once('test2').data('test2', 'foo'); 50 | 51 | // Create another once('test2') call. 52 | $('span').once('test2').data('test2', 'bar'); 53 | 54 | // The data should result to the first once() call. 55 | const data = $('span').data('test2'); 56 | assert.strictEqual(data, 'foo'); 57 | }); 58 | 59 | it('is called only once with an ID', () => { 60 | // Count the number of times once() was called. 61 | $('span').data('count', 0); 62 | 63 | // Create the once() callback. 64 | const callback = () => { 65 | // Increment the count variable stored in the data. 66 | $('span').data('count', $('span').data('count') + 1); 67 | }; 68 | 69 | // Call once() a bunch of times. 70 | for (let i = 0; i < 10; i++) { 71 | $('span').once('count').each(callback); 72 | } 73 | 74 | // Verify that it was only called once. 75 | const count = $('span').data('count'); 76 | assert.strictEqual(count, 1, 'It was called ' + count + ' times.'); 77 | }); 78 | 79 | it('is called only once without an ID', () => { 80 | // Count the number of times once() was called. 81 | $('span').data('once', 0); 82 | 83 | // Create the once() callback. 84 | const callback = () => { 85 | $('span').data('once', $('span').data('once') + 1); 86 | }; 87 | 88 | // Call once() a bunch of times. 89 | for (let i = 0; i < 10; i++) { 90 | $('span').once().each(callback); 91 | } 92 | 93 | // Verify that it was only called once. 94 | const count = $('span').data('once'); 95 | assert.strictEqual(count, 1, 'It was called ' + count + ' times.'); 96 | }); 97 | 98 | it('retrieves empty once data correctly', () => { 99 | // Verify that the element starts without the class. 100 | let hasData = $('span').data('jquery-once-test3'); 101 | assert(!hasData, 'Value not applied in the beginning.'); 102 | 103 | // Create one once() call. 104 | $('span').once('test3'); 105 | 106 | // Verify the data is applied. 107 | hasData = $('span').data('jquery-once-test3'); 108 | assert(hasData, 'The value is properly applied after once().'); 109 | }); 110 | 111 | it('calls removeOnce() correctly', () => { 112 | // Create one once() call. 113 | $('span').once('test4'); 114 | 115 | // Verify the data is applied. 116 | let hasData = $('span').data('jquery-once-test4'); 117 | assert(hasData, 'The value is properly applied after once().'); 118 | 119 | // Remove the once property. 120 | $('span').removeOnce('test4'); 121 | hasData = $('span').data('jquery-once-test4'); 122 | assert(!hasData, 'The value is properly removed when called removeOnce().'); 123 | }); 124 | 125 | it('calls findOnce() correctly', () => { 126 | // Append an additional span to the end. 127 | document.body.innerHTML += 'This is the Test 2.
'; 128 | 129 | // Create one once() call. 130 | $('span').once('test5').data('foo', 'bar'); 131 | 132 | // Find the once'd elements. 133 | $('span').findOnce('test5').each(function () { 134 | assert.strictEqual($(this).data('foo'), 'bar', 'Found correct span data.'); 135 | }); 136 | }); 137 | }); 138 | --------------------------------------------------------------------------------