This question has already been answered:

$(function() { //какой-то код }); 

Reported as a duplicate at Grundy. javascript Jan 20 '17 at 8:03 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • In this form, this is just an ordinary function wrapped in non-doing brackets, I suspect there should be some other code - andreymal
  • andreymal, for example, "$" - GoodNew5
  • But this changes absolutely everything and must be indicated in the question itself - andreymal
  • and what could be instead of "$"? - GoodNew5
  • Absolutely anything :) - andreymal

2 answers 2

This feature is a brief entry for

 $(document).ready(function(){ ... })` 

It is started as soon as DOM th can be manipulated.

Source: $ (document) .ready ()

    This function means that the code in it will be executed immediately after the page loads. Those. you specify functions in it and it will be executed immediately after the page loads.

    The code starts as soon as the DOM is ready to interact. Sometimes it's better to use window.onload , since This code is run after the page is fully loaded:

     window.onload = function() {}; 

    In the case of jQuery like this:

     $(window).load(function() {}); 
    • one
      The function from the question itself will not be executed - andreymal
    • And now, when the question has been changed, this answer is closer to the truth, but incomplete - andreymal
    • well, or: $ (function () {$ (window) .scroll (relocate);}); - GoodNew5
    • @ GoodNew5, no. This function is called when the window is scrolled - Yuri
    • one
      It is executed immediately after the entire DOM is loaded. And that's all. Not "often" any, but just like that. And the sentence "the code in it will be executed immediately after the page loads" - <b> wrong </ b>, the function does not do that. For this there is .onload . - VostokSisters