Check out the function bindReady in the source code.
They bind to the DOMContentLoaded event (or onreadystatechange on some browsers). They also have a fallback to the regular load event, in case the DOMContentLoaded isn’t supported or not fired for other reasons. On browsers supporting it, they use this call:
document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
On IE <9:
document.attachEvent( "onreadystatechange", DOMContentLoaded);
The second instance of DOMContentLoaded
in these calls is their own function – actually a reference to the ready
function right above bindReady
in the source code. This function will check that the DOM tree is actually done by checking for document.body
. If it doesn’t exist yet, they wait a millisecond (using setTimeout) and check again. When document.body exists, they traverse the list of callbacks you’ve set.