The difference between read in jquery and the OnLoad function in JavaScript _jquery

Source: Internet
Author: User

In JavaScript, the OnLoad function is most commonly used, and children's shoes, which involve almost JavaScript, have to be contacted. The function is to wait until the Web page is completely loaded and then execute the statements within the code block, because the sequence of the flow of the document is usually used when the header is loaded with JavaScript.

Copy Code code as follows:

Window.onload = function () {
Execute the code block here when the Web page is finished loading
};

And in jquery There is also a function that waits for the page to be loaded and executes the code block.
Copy Code code as follows:

$ (document). Ready (function () {
Execute the code block here when the Web page is finished loading
});

These two seemingly the same functions, but there is a big difference in reality.

OnLoad is not only performed after the DOM tree is created, but after all external resources have been loaded, and the entire page is displayed in the browser window. These resources not only include picture resources, but also include Flash video embedded on the page, and if there are too many pictures or flash, it will take a long time to load, which means it will delay the execution of the code block more time.

The Ready () method in jquery only needs to wait for the document structure to be fully parsed and the browser has transformed HTML into a DOM tree to execute the code block, noting that only the DOM, the pictures in the Web page, flash and other external resources are irrelevant.

This shows that the ready () method in jquery will shorten the wait time.

Of course there is another way, is to put the script behind the <body> tag, so that the Web page in accordance with the document flow order, will also reach the JavaScript in the onload or jquery ready () effect, and this way will be faster to show the content of the Web page.

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.