In jQuery, the ready function and window. onload are executed first. window. onload and ready

Source: Internet
Author: User

In jQuery, the ready function and window. onload are executed first. window. onload and ready

A. About $ (document). ready ():

$ (Document). ready () in jquery, what is the role of $ (document). ready? Can I use window. onload = function () {...} to implement it?

Here, we need to clarify the differences between the two.

We use window. onload = function (){...}, it is intended to execute function processing when the page is loaded, but these JS Code can only load all the content on the page (including banner advertisements in the header and all images). The reason for putting window. onload at the top is that when you run codes for the first time, the HTML 'document' has not been loaded yet.

$ (Document). ready () does not need to be loaded completely ". $ (Document ). ready () is executed after the DOM structure is loaded, while window. onload is executed after all files are loaded. Note the difference: one is that the DOM is loaded, and the other is that all files are loaded. Therefore, ready must occur before onload. Loading Large or many images on the page will delay onload execution. Using jquery ready can alleviate this problem.

We should note that jquery's ready refers to executing the specified function after the DOM model of the page is loaded. $ (Document) is often used ). ready () to replace window. onload is because it is executed after the dom model is loaded, while window. onload is executed only after the dom element is fully loaded.

B. About document. onload and window. onload:

There is no essential difference between document. onload and window. onload. They indicate that the specified function is executed after the page is loaded.

Make a comparison:

window .onload =function (){alert ("welcome");} $(document).ready( function (){  alert ("thanks for visiting!"); });

After running, you will find that $ (document). ready () is executed first.

The above is all about this article. For more information about jQuery syntax, see jQuery 1.10.3 online manual.

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.