$ (function () {}) and $ (document). Ready (function () {})

Source: Internet
Author: User

holysonll.blog.163.com/blog/static/2141390932013411112823855/

the difference between Document.ready and onload--javascript document load completion eventThere are two kinds of events for page load completionOne is ready, indicating that the document structure is loaded (not including non-text media files such as pictures)The second is onload, which indicates that all elements, including pictures and other files, are loaded and completed. A lot of people who use JQ have started writing scripts like this:$(function() { //Do something});In fact, this is the abbreviation of JQ Ready (), which is equivalent to:$(document). Ready(function() { //do something})//or the following method, the default parameter of Jquer is: "Document";$(). Ready(function() { //do something})This is the method of JQ ready (), which is the DOM, and his role or meaning is that the DOM can be manipulated after the DOM has been loaded. in general, the first page response loading order is: Domain name resolution-loading html-loading JS and css-loading pictures and other information. then Dom ready should be able to manipulate the DOM between "Loading JS and css" and "Loading pictures and other information".    1.window.onload Method  ⑴ execution time: All elements in a Web page, including all associated files of an element, are fully loaded into the browser before they are executed, that is, JavaScript can now access all the elements in the Web page. Window.onload=function () {$ (window). Load (function () {Writing codeequivalent to //write code }  });⑵ Multiple use:the onload event of JavaScript can only hold a reference to a function at a time , and he will automatically overwrite the previous function with the last function.

function one() {

alert("one");

}

function () {

alert("both");

}

Window. OnLoad=one;

Window. OnLoad=both;

Only one after running the code

2.$ (document). Ready () Method  ⑴ Execution Time: it can be called when the DOM is fully ready. (This does not mean that the files associated with these elements have already been downloaded)For example: $ (document). The Ready () method can be manipulated knowing that the DOM is in place, without waiting for all pictures to be downloaded.⑵ Multiple use:

function one() {

alert("one");

}

function () {

alert("both");

}

$(document). Ready(function() {

One();

});

$(document). Ready(function() {

both();

});

After running the code

First: One

First:

(go) $ (function () {}) and $ (document). 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.