According to the idea, this should not happen, but judging by console.log() when you repeatedly access the page from time to time $(window).load is executed before the $(document).ready execution is completed. What is it? Because of page caching?

UPD: <meta http-equiv="Cache-Control" content="no-cache" /> does not help, so it's not the cache

UPD2: when reloading the page, everything always starts to work correctly

  • I have already believed that this is not the case except for one case - when a subscription is made from the handler itself. So it is worthwhile to provide a minimal reproducible example . - Qwertiy

5 answers 5

The DOMContentLoaded event that "calls" the document ready() method always occurs earlier based on the documentation: https://developer.mozilla.org/ru/docs/Web/API/Document/readyState

Therefore, we need your example.

 $(window).load(function() { console.log("window"); }); $(document).ready(function() { console.log("document"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

  • The code is quite cumbersome and is crammed with a large number of ajax requests. I'm not sure that when copying the problem may persist. I think that someone has encountered similar behavior and can tell from experience. For example, I have a lot of windows.load and document.ready constructions in my script. Could this somehow affect the order of execution? - Kula Hula
  • @ KonstantinKuznetsov But what does upd2 mean "after reloading the page"? - lampa

$(window).load

The load event occurs when the element itself and all its children are fully loaded. This event can occur on elements that have URL fields (window object, images, scripts, frames).

Note 1 : in some cases, if the picture is contained in the browser's cache, the load event may not occur. For such a case, you can use the special event event.special.load , which is defined in a small plugin.

Note 2 : if you do not require the readiness of multimedia files, it is better to use the .ready() method, which installs the document structure readiness handler, which occurs before the start of downloading multimedia files.

It's all very clear if we write $(window).load then the code written inside this construct will start working when the entire DOM including the images is ready . It is more logical to make such a call when it is necessary to work with images (calculation of image sizes or something else).


$(document).ready

$(document).ready code inside the block will start executing immediately after the DOM is ready, except for the images . The specified code will be executed immediately after the DOM is ready, without waiting for the complete loading of images. Calling $(document).ready several times will cause calls to be executed one after the other, in a sequence from top to bottom.


$(window).load or $(document).ready ?

Although it will take a lot of effort to reproduce this situation, there are still some nuances. Sometimes, this occurs when inside the ready event triggers a subscription to the load for example, or vice versa, which leads to strange behavior.

If you rely on some other answers, such as:

then you come to the conclusion that the $(window).load event $(window).load should always occur after $(document).ready . But, the presence of such behavior is available.

Now we will think about page caching. Yes, indeed, after the images have been cached, all the code you have written is likely to start executing sequentially, from top to bottom. Thus, there will be situations when $(window).load and $(document).ready both of these blocks will wait only for DOM loading, and if there is also a subscription to an event inside the event, then there will be a collision, and a worth $(window).load before $(document).ready executes earlier. In any case, a collision does not just happen and you need to fix the code and refactor it.

Useful links for familiarization:

  • "in this way there will be situations when standing $ (window) .load before $ (document) .ready is executed earlier" - this statement is doubtful . - Qwertiy
  • @Qwertiy, when load and ready wait for the same thing - only DOM loads, and there are several such blocks in a row - then they will be executed sequentially and in this case it will turn out that the load will be executed before the next ready . If there were only ready - everything would be done normally, then load with caching causes collisions. - Denis Bubnov
  • Can an example (or at least a detailed description) how to reproduce? - Qwertiy
  • @Qwertiy, reproduce ... you need a bunch of code with multiple load and ready , and the page must have resources, such as images, so that they are loaded. I saw a similar thing, in one project, the integration of blocks helped. Such code, mixed with load and ready and not in single copies, will not bring good to the good - that’s a fact. In the snippet, this is unlikely to be reproduced: ( - Denis Bubnov
  • @Qwertiy, by the way, this is probably unlikely to be done only if there is no subscription from the handler ... and yes, after ready should be a load ... but I remember what happened, that everything went wrong. would. Without the author's code is unlikely to be able to reproduce. - Denis Bubnov

I carefully looked at the Network in DevTools and solved the problem by removing the ajax request that created the image and ran for 10 seconds.

Here is what happened: Ajax sending a request and not receiving an answer for some reason thought that DOM-built and it was time to trigger a load event, and after 10 seconds a response came from what seemed to be a dead request, so ready was done later load =)

    $(document).ready - Works when the browser has loaded html and built a DOM дерево , and $(window).load - it works when all the resources are loaded (pictures, etc.), that is, theoretically $(document).ready works before, but after reloading the page, the resources (pictures) of us can be cached, in which case $(window).load can start to be executed approximately in conjunction with $(document).ready . $(document).ready .

    • Together? Js single threaded. And yet, there may be no pictures and resources at all - but this does not affect the order. - Qwertiy
    • "about jointly" here meant that events would work approximately jointly and the code that was above would work earlier. - Yevhen Bondarenko

    Most likely a subscription to an event that arises later is already done from another handler of the load event. Other ways to reproduce this situation is unknown to me.