window load inside a document ready?

前端 未结 4 1920
盖世英雄少女心
盖世英雄少女心 2020-12-13 05:03

Sorry if this has been answered before but all searches talk about the differences, not about using the two together, if possible.

Simply, can $(window).load.(

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 05:52

    I ran into this problem recently... from jQuery version 3, we can no longer put $(window).on('load') inside document.ready()... because ready handler are called asynchronously, which means ready can be called after load.

    From jQuery Core Team: Github: jQuery 3 issues

    To be clear, we understand what's causing this. We recently made ready handlers fire asynchronously. This has advantages that are hard to give up. The disadvantage is that the ready handler can sometimes fire after the load event if the load event fires quickly enough. The side effect you're seeing in this issue is that you're binding a load event handler after the load event has already fired.

    The fix is to bind the load outside of ready.

    This is how the two methods should be called:

    $(function() {
      // Things that need to happen when the document is ready
    });
    
    $(window).on("load", function() {
      // Things that need to happen after full load
    });
    

提交回复
热议问题