How to find out the first DOM ready state

I am trying to get the first DOM ready state. second, third, etc. I'm not interested, but the first. is there any trick to get the first DOM ready state?

$(document).ready(function() { // is it the first ready state? }); 
+2
source share
2 answers

Ah, you are using jQuery. Look at the docs : there is only one ready event! I will never shoot several times. Internally , this is even handled by Promise , so it cannot fire several times.

+1
source

There are 4 possible readiness values:

  • uninitialized - not yet loaded
  • Download - Download
  • interactive - Loaded enough, and the user can interact with it.
  • complete - fully loaded

To see this value, use this code:

 document.onreadystatechange = function () { if (document.readyState === YourChoice) { // ... } } 

I could not catch uninitialized readyState. (but why do I need this?)

If you need a listener to fully load the DOM, use:

 document.addEventListener('DOMContentLoaded', YourListener); 

or

 document.addEventListener('load', YourListener); 

or even

 window.onload = YourListener; 

for jquery:

 $(document).on("DOMContentLoaded", function() { }); 

or

 $(document).on("load", function() { }); 

or

 $(window).on("load", function() { }); 
+3
source

Source: https://habr.com/ru/post/979131/


All Articles