Skip to content

Instantly share code, notes, and snippets.

@dperini
Created June 25, 2013 12:05
Show Gist options
  • Save dperini/5857960 to your computer and use it in GitHub Desktop.
Save dperini/5857960 to your computer and use it in GitHub Desktop.
Here is an example on how to feature detect an Asynchronous event using a Synchronous event. Being able to capture 'load' events of each resource in a web page is a very interesting feature. All resources could be logged, or modified with just one 'load'' capturing event handler. This works for external link elements (CSS), for iframes, images, …
/*
*
* Feature detect support for capturing load events
*
* Author: Diego Perini
* Updated: 2013/06/25
*
* the feature test can be performed before
* both 'load' and 'DOMContentLoaded' events
*
* a synchronous event is triggered to test
* browsers handling of the dispatched event
*
* IE6/7/8 have no capturing event support at all
* all other browsers should support this feature
*
* document.addEventListener('load', handler, true);
*
*/
function has_load_capture_support() {
var d = document, event,
root = d.documentElement,
done = false, support = false;
if (d.addEventListener) {
event = d.createEvent('Event');
function handler(event) {
if (event.target == root && event.eventPhase == 1) {
support = true;
d.removeEventListener('load', handler, true);
}
}
d.addEventListener('load', handler, true);
event.initEvent('load', true, true);
done = root.dispatchEvent(event);
}
return done && support;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment