jquery ready vs domcontentloaded
Assuming browser that supports the event:
- The real event can support any
document
. jQuery will only use thedocument
it was loaded in, no matter what you pass to it. - jQuery will fire the event asynchronously even if the event has already happened. Attaching
'DOMContentLoaded'
event will do nothing if the event has already happened.
There is no delay in these browsers, see http://jsfiddle.net/rqTAX/3/ (the offsets logged are in milliseconds).
For browsers that don't support the event, jQuery's will obviously work for them as well. It will use a hacky mechanism that is not the same as the real DOMContentLoaded
and will not necessarily fire as soon as the real DOMContentLoaded
would:
// The DOM ready check for Internet Explorer
function doScrollCheck() {
if ( jQuery.isReady ) {
return;
}
try {
// If IE is used, use the trick by Diego Perini
// http://javascript.nwbox.com/IEContentLoaded/
document.documentElement.doScroll("left");
} catch(e) {
setTimeout( doScrollCheck, 1 );
return;
}
// and execute any waiting functions
jQuery.ready();
}