jquery load () usage and ready () usage and differences

Source: Internet
Author: User
Tags bind html tags php tutorial

$ (document). Ready (FN) and $ (window). Load (FN), the difference is: $ (document). Ready (FN) occurs when the HTML of the page itself is loaded, $ (window). Load (FN) You will wait until everything in the HTML tags referenced in the Web page, inline objects (such as Flash), IFRAME, and so on is loaded and rambling.

In general, it is a bit late to execute a program when all the elements of a Web page are loaded, because before that, the user can click on the operation page to jump off the control of our program. So, we almost all put the program in $ (document). Read (function () {...}) (That is, the $ (function () {...})). and $ (window). Load (FN) is appropriate to perform actions that you want to wait for all of the files or elements to be loaded, for example, to check the length of the file.

Binding the load's handler function before the element is fully loaded will trigger the load after he has finished loading. If you bind later, it will never be triggered. So do not bind the Load event in $ (document). Ready (), because jquery binds the Load event after all the DOM loads have been completed.

The full format for calling the Load method is: Load (URL, [data], [callback]),

which

URL: Refers to the address of the file to be imported.
Data: Optional parameters, because load can not only import static HTML files, but also import dynamic scripts, such as PHP tutorial files, so to import the dynamic file, we can put the parameters to pass here.
Callback: optional parameter; is another function that is executed after the Load method is invoked and the server responds.
One: How to use Data

1. Load a php file, the PHP file does not contain the pass parameter $ ("#myid"). Load ("test.php");

Import test.php Run results in an element with ID #myid
2. Load a PHP file that contains a pass parameter

$ ("#myid"). Load ("test.php", {"name": "Adam"});

The imported PHP file contains a pass-through parameter, similar to the following: Test.php?name=adam
3. Load a PHP file that contains multiple pass parameters. Note: The parameters are separated by commas

$ ("#myid"). Load ("test.php", {"name": "Adam", "Site": "61dh.com"});

The imported PHP file contains a pass-through parameter, similar to the following: test.php?name=adam&site=61dh.com
4. Load a php file, the php file with an array as the passing parameters

$ ("#myid"). Load ("test.php", {' myinfo[] ', ["Adam", "61dh.com"]});

The imported PHP file contains an array pass parameter.

Ready method


Simple Way
$ (document). Ready (function () {
Writing code
})
The shorthand method is
$ (function () {
Writing code
})
In addition, $ (document) can also be abbreviated to $ (). When $ () has no parameters, the default argument is "document", so it can be abbreviated as:
$ (). Ready (function () {
Writing code
})
3 different ways to 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.