Differences between JQuery's ready function and JS's onload

Source: Internet
Author: User

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.