The difference between $ (document) .ready () and the window.onload

The difference between $ (document) .ready () and the window.onload

$ (Document) .ready () method and window.onload documents are ready, when the page is loaded function parameters in the execution.

The former is a jQuery method, which is a JavaScript method. There are three differences between them:

The difference between 1: short and not abbreviated

in jQuery $ (document) .ready () can be abbreviated to $ ().

// $ (document) .ready () may be abbreviated $ () 
$ (function () { 
    Alert ( "111"); 
})

Native js in window.onload not be abbreviated.

Abbreviations not // 
the window.onload = function () { 
    Alert ( "111"); 
}

2 the difference between: the difference between the number of

In jQuery .ready () method can write more.

$ (function () { 
    the console.log ( "statement. 1"); 
}) 
            $ (function () { 
    the console.log ( "statement 2"); 
})

In the console output statement. Both outputs.

 

 

window.onload you can only write one.

= function the window.onload () { 
    the console.log ( "statement 1"); // is covered only output sentence 2 
}; 
the window.onload = function () { 
    the console.log ( "statement 2"); 
};

Code output only a top-down operation

 

 

 The difference between 3: Execution timing is not the same

.ready () method is that all DOM document structure receipt pages immediately after completion of execution, may be associated DOM element content (images, flash, video, etc.) have not been loaded on the implementation.

window.onload must wait for all the content on the page is loaded (including images, flash, etc. Screen to perform).

Below this comparison does not seem very strict not to do with the difference between the amount of code you can only do some comparison between them

= function the window.onload () { 
    Alert ( "all page elements loaded"); 
} 
$ (function () { 
    Alert ( "DOM structure loaded page");    
})

the jQuery .ready () method first came out, executed earlier advance to achieve the effect of interface

window.onload method pop-up

 

These are the difference between JavaScript and ready method of jQuery

If wrong, please correct me: D

Guess you like

Origin www.cnblogs.com/449house/p/11925549.html