jQuery - What are differences between $(document).ready and $(window).load?

前端 未结 8 2188
旧巷少年郎
旧巷少年郎 2020-11-22 02:45

What are differences between

$(document).ready(function(){
 //my code here
});

and

$(window).load(function(){
  //my code h         


        
8条回答
  •  悲哀的现实
    2020-11-22 03:37

    $(document).ready(function() {
      // executes when HTML-Document is loaded and DOM is ready
      console.log("document is ready");
    });
    
    
    $(window).load(function() {
      // executes when complete page is fully loaded, including all frames, objects and images
      console.log("window is loaded");
    });

    Query 3.0 version

    Breaking change: .load(), .unload(), and .error() removed

    These methods are shortcuts for event operations, but had several API limitations. The event .load() method conflicted with the ajax .load() method. The .error() method could not be used with window.onerror because of the way the DOM method is defined. If you need to attach events by these names, use the .on() method, e.g. change $("img").load(fn) to $(img).on("load", fn).1

    $(window).load(function() {});
    

    Should be changed to

    $(window).on('load', function (e) {})
    

    These are all equivalent:

    $(function(){
    }); 
    
    jQuery(document).ready(function(){
    });
    
    $(document).ready(function(){
    });
    
    $(document).on('ready', function(){
    })
    

提交回复
热议问题