The Ready function in jquery and window.onload who first executes _jquery

Source: Internet
Author: User

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

$ (document) in jquery. Ready (), that $ (document). Ready () What is the role? Is it possible to use window.onload = function () {...} To achieve it?

Here, we want to make clear the difference between the two.

We use the window.onload = function () {...} to perform the processing of the function while the page is loaded, but these JS codes are only performed when the entire contents of the page are loaded (including the header's banner ads, all pictures). The reason to put window.onload on top is that when you first run codes, HTML ' document ' hasn't been loaded yet.

and $ (document). Ready () does not need to be loaded so "completely". $ (document). Ready () is executed after the DOM structure is loaded, and the window.onload is executed after all the files have finished loading, note the difference, one is the DOM is loaded, and one is all the files are loaded. Therefore, ready must have occurred before the onload, the page load large or many pictures and the onload execution delay, using jquery ready can alleviate this problem.

We should bear in mind that jquery's ready is the execution of the specified function after the page's DOM model has been loaded. The use of $ (document). Ready () is often substituted for window.onload because it executes after the DOM model is loaded, and Window.onload executes after the DOM element has been loaded completely.

B. About Document.onload and window.onload:

There is no essential difference between document.onload and window.onload, which means that the specified function is executed after the page load completes.

Make a comparison:

window. onload =function () {alert ("Welcome");}
 $ (document). Ready (
 function () {
  alert (' for visiting! ');
 }
);

After running, you will find $ (document). Ready () executed first.

The above is the entire contents of this article, to learn more jquery syntax, you can see: "JQuery 1.10.3 Online Handbook", but also hope that we support the cloud-Habitat community.

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.