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

前端 未结 8 2183
旧巷少年郎
旧巷少年郎 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:30

    From the jQuery API Document

    While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. When using scripts that rely on the value of CSS style properties, it's important to reference external stylesheets or embed style elements before referencing the scripts.

    In cases where code relies on loaded assets (for example, if the dimensions of an image are required), the code should be placed in a handler for the load event instead.


    Answer to the second question -

    No, they are identical as long as you are not using jQuery in no conflict mode.

提交回复
热议问题