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