Differences between JQuery's ready function and JS's onload:
1. execution time
Window. onload can only be executed after all elements including images in the page are loaded.
$ (Document). ready () is executed after the DOM structure is drawn. You do not have to wait until the loading is complete.
2. different numbers of codes
Window. onload cannot be written at the same time. If there are multiple window. onload methods, only one
$ (Document). ready () can be compiled at the same time, and can all be executed.
3. Simplified writing
Window. onload is not simplified
$ (Document). ready (function () {}) can be abbreviated to $ (function (){});
Example:
Copy codeThe Code is as follows:
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN"
Http://www.w3.org/TR/html4/loose.dtd>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> Differences Between ready and onload in js </title>
<Script type = "text/javascript" src = "jquery-core/jquery-1.8.0.js"> </script>
<Script type = "text/javascript">
// Execute the jsFunction1 function for the onload attribute of the window object in js.
Window. onload = jsFunction1;
// Execute the jsFunction2 function for the onload attribute of the window object in js.
Window. onload = jsFunction2;
// Run the jqFunction1 function in the ready method of jquery.
$ (Document). ready (jqFunction1 );
// Run the jqFunction2 function in the ready method of jquery.
$ (Document). ready (jqFunction2 );
// JsFunction1 Function
Function jsFunction1 (){
Alert ("jsFunction1 ");
}
// JsFunction2 Function
Function jsFunction2 (){
Alert ("jsFunction2 ");
}
// JqFunction1 Function
Function jqFunction1 (){
Alert ("jqFunction1 ");
}
// JqFunction2 Function
Function jqFunction2 (){
Alert ("jqFunction2 ");
}
</Script>
</Head>
<Body>
<H1> Differences Between ready and onload in js </Body>
</Html>