Jquery load event (callback/data) usage and precautions

Source: Internet
Author: User

If it is bound to a window object, it is triggered after all content is loaded, including the window, frame, object, and image. If it is bound to an element, it is triggered after the content of the element is loaded.

Note:: The load handler is triggered only after the element is fully loaded. If it is bound later, it will never be triggered. Therefore, do not bind the load event in $ (document). ready (), because jQuery will bind the load event after all DOM loads are completed. Therefore, when using the load method, try to write the load method at the top of the page.

The complete format for calling the load method is: load (url, [data], [callback]),
Where:
• Url: the address of the file to be imported.
• Data: an optional parameter. Because Load can not only import static html files, but also dynamic scripts, such as PHP files, when importing dynamic files, we can put the parameters to be passed here.
• Callback: an optional parameter. It is another function that is executed after the load method is called and the server response is obtained.

I. How to use data
1. load a php file without passing the parameter $ ("# myID"). load ("test. php ");
// Import the result of test. php running in the element whose id is # myID 2. Load a php file, which contains a passing Parameter
$ ("# MyID"). load ("test. php", {"name": "Adam "});
// The imported php file contains a passing parameter, similar to: test. php? Name = Adam3. load a PHP file containing multiple passing parameters. Note: parameters are separated by commas (,).
$ ("# MyID"). load ("test. php", {"name": "Adam", "site": "61dh.com "});
// The imported php file contains a passing parameter, similar to: test. php? Name = Adam & site = 61dh. com4. load a PHP file. The php file uses an array as the passing parameter.
$ ("# MyID"). load ("test. php", {'myinfo [] ', ["Adam", "61dh.com"]});
// The imported PHP file contains an array for passing parameters. Note: using load, these parameters are passed in POST mode. Therefore, GET parameters cannot be obtained in test. php.

Ii. How to Use callback
For example, you can use the callback function to slowly display the loaded content after the load method obtains the server response. The Code is as follows:
Copy codeThe Code is as follows:
$ ("# Go"). click (function (){
$ ("# MyID"). load ("welcome. php", {"lname": "Cai", "fname": "Adam", function (){
$ ("# MyID"). fadeIn ('low ');}
);
});

Note:
Add a space in the load url and you will be able to follow the selector.
For example:
Copy codeThe Code is as follows:
$ ("Body"). load ("test.html # ");

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.