$ (window). Load (function () {}) and $ (document). The difference between ready (function () {})

Source: Internet
Author: User

In JavaScript

function(){   alert(“text1”); }; 

is equivalent to in jquery.

$(window).load(function(){    alert("text1");});

They are all used to run the alert function within a function when all elements of the page, including external reference files, images, etc. are loaded. The Load method can only be executed once, if more than one is written in the JS file, only the last one can be executed. (Beginners often in the page structure of the element directly write behavior function, this is very ugly, you can use selectors and so on, in the JS code to control the elements of the behavior.) )

In jquery

$(document).ready(function(){    alert("text2");});

Equal to (simplified notation)

$(function(){    alert("text2");});

They are all used to execute an internal alert function when a page's standard DOM element is parsed into a DOM tree. This function can be written in the JS file many times, for many people to write a common JS has a great advantage, because all the behavior function will be executed. and $ (document). The Ready () function can be executed after the HMTL structure has been loaded, without the time-consuming tasks such as large file loading or non-existent connections, and the efficiency is high.

Of course, according to the needs of the project to use, such as sometimes the picture or the important information does not come out, will be misleading to user operation, or need to obtain some pictures of high-wide data after the execution of the behavior function, you have to use window.onload.

$ (window). Load (function () {}) and $ (document). The difference between ready (function () {})

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.