https://github.com/arnaud-lb/jquery.event.queuehandler
Queues handlers to be ran after the current event's handlers have been ran
https://github.com/arnaud-lb/jquery.event.queuehandler
Last synced: 3 months ago
JSON representation
Queues handlers to be ran after the current event's handlers have been ran
- Host: GitHub
- URL: https://github.com/arnaud-lb/jquery.event.queuehandler
- Owner: arnaud-lb
- Created: 2011-09-10T14:50:49.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2011-09-10T20:04:45.000Z (over 13 years ago)
- Last Synced: 2025-01-10T19:23:28.087Z (5 months ago)
- Language: JavaScript
- Homepage:
- Size: 93.8 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
Awesome Lists containing this project
README
jQuery plugin to queue a function to be ran after the current event's handlers have been ran.
### API
- event.queueHandler(callback): queue a function to be ran after the current event's handlers have been ran
### Usecase
``` javascript
$(document).delegate('a[rel="external"]', 'click', function(event) {
window.open(this.href);
});
$(document).delegate('a', 'click', function(event) {
event.stopImmediatePropagation();
});
```Problem in this code: The second event handler can't avoid the first event handle from being run.
Obvious solution: use `setTimeout()` in the first event handler and test for `event.isImmediatePropagationStopped()`:
``` javascript
$(document).delegate('a[rel="external"]', 'click', function(event) {
setTimeout(function() {
if (event.isImmediatePropagationStopped()) return;
window.open(this.href);
}, 0);
});
```Problem: `window.open()` is blocked
Solution: queue a function to be ran just after other event handlers:
``` javascript
$(document).delegate('a[rel="external"]', 'click', function(event) {
event.queueHandler(function() {
if (event.isImmediatePropagationStopped()) return;
window.open(this.href);
});
});
```