What is the difference between $ (document) .ready () and the inclusion of a script at the end of the body?

What is the difference between executing a JavaScript function in jQuery $ (document) .ready () and including it in HTML in script tags at the end of the body?

Thank,

DLiKS

+3
source share
3 answers

JavaScript code inside tags <script>is evaluated (executed) immediately. Please note: in this case, the page has not yet been analyzed (completely), and the DOM is not yet ready.

JavaScript code inside the jQuery callback ready()is evaluated in the DOMContentLoaded event, which occurs after all the HTML source code has been parsed by the browser.
About this event: https://developer.mozilla.org/en/Gecko-Specific_DOM_Events

, :

$(function() {
    // code
});

, SO, , , : JavaScript - ?

+7
  • A script .
  • $. ready ( css ).
  • Body.load .
+3

SO .

script, (</body>), , $(document).ready(function(){}); <head>.

+1
source

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


All Articles