The difference between JS entry function and jquery entry function

Source: Internet
Author: User

JS Window.onload Event must wait until all content, as well as external images and other files are loaded before the execution.

The jquery entry function is executed after all tags have been loaded.

Then, an explanation of the implementation of the jquery entry function is derived from a cover problem of JS.

JS Entrance function window.onload function has a cover problem, when there are 2 window.onload functions in the document, the latter will overwrite the former, resulting in the function can not be implemented. But jquery has no such problem, it is important because the jquery entry function is just a call to the encapsulated method, except that the arguments are different.

Finally, talk about the jquery entry function (document). Ready ();) implementation.

First, we'll create a function variable called $ in the script tag, and then call it later. Now it doesn't have any effect, because this call is just an empty object, without any arguments. If we want to continue calling after $ (), we need to do something about it. Let's see if there is a ready method in the jquery entry function, we can create an object's property called Ready, then point to an anonymous function, and now we can call it through $ ().

Test with a specific function:

Next, the jquery entry function can pass a function (passed in as a variable) in, that is, $ (). Ready (function () {}) and then received with a parameter (func) variable, then strictly execute the received method, just call (Func ()) on the line.

Let's look at the document parameter before the jquery entry function, which is $ (document). Ready (function () {}), which is exactly the same as the entry function for jquery. It also needs to be received with a variable. Finally, just check the ready function to see if the OnLoad event has (or is not) assigned:

(1) has (is assigned), receives an old function, re-assigns the OnLoad event, and then first calls the new function, then calls the old function, so there is no problem of overwriting, both of them are implemented.

(2) No, directly assigns an onload event.

Finally, let's test, because document does not have the OnLoad event, we can use window to try.

This avoids the problem of function coverage.

The difference between JS entry function and jquery entry 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.