Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is DOMContentLoaded event EXACTLY the same as jQuery's .ready() function?

I have replaced window.addEventListener('DOMContentLoaded', function() {}); with jQuery's $(document).bind('ready', function() {});, because first one failed to work on IE < 9 and I did not wanted to play with .attachEvent() for that dummy browser, if I could have this nicely covered by jQuery itself.

Shortly after replacement, I noticed that DOMContentLoaded event was always fired around 0-2 miliseconds after page load / refresh (at least this is what was logged by my logging script), while .ready() always requires at least 15-20 miliseconds, after page refresh, to be fired (again - as logged by script).

I'm asking purely for feeding my curiosity, why there is such "significant" delay? Of course, there is no problem for me, that jQuery is firing that event later. It is just, that because I want to know ALL the answers (and rule the world! :]), I can't sleep with that! :]

EDIT: in .ready() function doc some user (Nick (of Nexxar)) points out that: "jQuery simulates the non existing "DOMContentLoaded" event on IE, but the used mechanism fires much later than the event used on other browsers". Maybe this is the same, I'm asking for?

like image 625
trejder Avatar asked Jul 17 '12 13:07

trejder


People also ask

Is DOMContentLoaded the same as document ready?

ready() method differs in an important and useful way: If the DOM becomes ready and the browser fires DOMContentLoaded before the code calls . ready( handler ) , the function handler will still be executed. In contrast, a DOMContentLoaded event listener added after the event fires is never executed.

What is the DOMContentLoaded event?

The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. A different event, load , should be used only to detect a fully-loaded page.

What is the difference between onload event and DOMContentLoaded event?

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images. This is in contrast to DOMContentLoaded , which is fired as soon as the page DOM has been loaded, without waiting for resources to finish loading.

What is difference between $( function () and document Ready?

So technically they are both the same. Not major difference between these two declaration. They used based on weather you use JavaScript then you should use $(document). ready declaration in other case you use jQuery library which is a part of JavaScript then you should use $(function) declaration.


2 Answers

Assuming browser that supports the event:

  1. The real event can support any document. jQuery will only use the document it was loaded in, no matter what you pass to it.
  2. 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(); } 
like image 172
Esailija Avatar answered Sep 22 '22 23:09

Esailija


jQuery simulates this event by binding to the document's readystatechange event, which is the standard way of simulating DOMContentLoaded in oldIE.

According to the jQuery source, that event fires "late" but before window.onload. However, I can't find when that event fires exactly. DOMContentLoaded fires when the DOM is built and ready for scripting, so readystatechange fires after that; perhaps it waits for layout rendering or styling something like that, or the event is triggered later in the rendering/layout process?

Regardless, it will likely fire after DOMContentLoaded, likely due to when IE decides to update the document's readyState to "complete."

(If anyone has a definite answer, post a comment and I'll update this answer; I'd love to know when exactly it fires myself, and I can't find that answer in any documentation or on any sites I would expect like Quirksmode.)

like image 26
ajm Avatar answered Sep 21 '22 23:09

ajm