How do you know if a document is ready

I have JavaScript that generates HTML blocks. This script is sometimes called somewhere at runtime, and sometimes even before the document is loaded. I want a script that can tell if a document is ready. If so, generate HTML, otherwise add the document.ready () function. What is jQuery best way to find out if a document is loaded?

+3
source share
6 answers

It's safe to always wrap HTML generation code in $ (document) .ready (). If the document is ready, the newly registered callback $ (document) .ready () will be executed immediately.

+1
source
$(document).ready(
    function() {
        //code to execute once the page is loaded
    }
);
+7
source

load :

$(window).load(function(){
  // your code...
});

load DOM, , .

+4

DOM

$(document).ready( function() {
  // your function
});

or a download event to wait for a download absolutely absolutely

$(document).load( function() {
  // your function
});

If you don’t know that you need to use the load event, I would use a ready-made one (which, I believe, is a DOMContentLoaded event).

+2
source

http://www.learningjquery.com/2006/09/introducing-document-ready

 $(document).ready(function() {
   // put all your jQuery goodness in here.
 });
0
source
$(function() {
    // document has loaded at this point
});
0
source

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


All Articles