The Ajax--load method in jquery source code

Source: Internet
Author: User

The load () method is the simplest and most common method of jquery, which can be loaded with remote HTML code and inserted into the DOM, with the following structure:

Load (URL [. data] [. Callback])

The parameters are explained as follows:

Parameter name Type Description
Url String Request the URL address of an HTML page
Data (optional) Object Key/value data sent to the server
Callback (optional) Function callback function when the request completes, whether the request succeeds or fails

The source code for load is as follows: (Source directory:jquery/src/ajax/load.js)

define ([".. /core ",    ".. /core/parsehtml ",    ".. /ajax ",    ".. /traversing ",    ".. /manipulation ",    ".. /selector "], function(jQuery) {"Use Strict";/** * Load a URL into a page*/JQuery.fn.load=function(URL, params, callback) {varselector, type, response, self= This, off= Url.indexof ("" ); if(Off >-1) {Selector=Jquery.trim (Url.slice (off)); URL= Url.slice (0, off); }    //If It ' s a function    if(Jquery.isfunction (params)) {//We assume that it ' s the callbackcallback =params; Params=undefined; //Otherwise, build a param string}Else if(Params &&typeofparams = = = "Object") {type= "POST"; }    //If We have elements to modify, make the request    if(Self.length > 0) {Jquery.ajax ({url:url,//If "Type" variable is undefined and then "GET" method would be used.            //Make value of this field explicit since            //user can override it through Ajaxsetup methodType:type | | "GET", DataType:"HTML", Data:params}). Done (function(responsetext) {//Save response for use with complete callbackResponse =arguments; Self.html (Selector?//If A selector is specified, locate the right elements in a dummy div                //Exclude scripts to avoid IE ' Permission Denied ' errorsJQuery ("<div>"). Append (jquery.parsehtml (responsetext)). Find (selector)://Otherwise Use of the full resultresponsetext); //If The request succeeds, this function gets "data", "status", "JQXHR"        //But they is ignored because response was set above.        //If It fails, this function gets "jqxhr", "status", "Error"}). Always (Callback &&function(JQXHR, status) {Self.each (function() {callback.apply ( This, Response | |[Jqxhr.responsetext, Status, Jqxhr]);        } );    } ); }    return  This;};} );

The whole source works as follows:

The Ajax--load method in jquery source code

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.