Skip to content

Commit 95291b4

Browse files
markelogrwaldron
authored andcommitted
jQuery.fn.load optimization
1 parent 6bed348 commit 95291b4

File tree

1 file changed

+38
-47
lines changed

1 file changed

+38
-47
lines changed

src/ajax.js

Lines changed: 38 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -158,72 +158,63 @@ jQuery.fn.extend({
158158
load: function( url, params, callback ) {
159159
if ( typeof url !== "string" && _load ) {
160160
return _load.apply( this, arguments );
161+
}
161162

162163
// Don't do a request if no elements are being requested
163-
} else if ( !this.length ) {
164+
if ( !this.length ) {
164165
return this;
165166
}
166167

167-
var off = url.indexOf( " " );
168+
var selector, type,
169+
self = this,
170+
off = url.indexOf(" ");
171+
168172
if ( off >= 0 ) {
169-
var selector = url.slice( off, url.length );
173+
selector = url.slice( off, url.length );
170174
url = url.slice( 0, off );
171175
}
172176

173-
// Default to a GET request
174-
var type = "GET";
177+
// If it's a function
178+
if ( jQuery.isFunction( params ) ) {
175179

176-
// If the second parameter was provided
177-
if ( params ) {
178-
// If it's a function
179-
if ( jQuery.isFunction( params ) ) {
180-
// We assume that it's the callback
181-
callback = params;
182-
params = undefined;
180+
// We assume that it's the callback
181+
callback = params;
182+
params = undefined;
183183

184-
// Otherwise, build a param string
185-
} else if ( typeof params === "object" ) {
186-
type = "POST";
187-
}
184+
// Otherwise, build a param string
185+
} else if ( typeof params === "object" ) {
186+
type = "POST";
188187
}
189188

190-
var self = this;
191-
192189
// Request the remote document
193190
jQuery.ajax({
194191
url: url,
192+
193+
// if "type" variable is undefined, then "GET" method will be used
195194
type: type,
196195
dataType: "html",
197-
data: params,
198-
// Complete callback (responseText is used internally)
199-
complete: function( jqXHR, status, responseText ) {
200-
// Store the response as specified by the jqXHR object
201-
responseText = jqXHR.responseText;
202-
// If successful, inject the HTML into all the matched elements
203-
if ( jqXHR.isResolved() ) {
204-
// #4825: Get the actual response in case
205-
// a dataFilter is present in ajaxSettings
206-
jqXHR.done(function( r ) {
207-
responseText = r;
208-
});
209-
// See if a selector was specified
210-
self.html( selector ?
211-
// Create a dummy div to hold the results
212-
jQuery("<div>")
213-
// inject the contents of the document in, removing the scripts
214-
// to avoid any 'Permission Denied' errors in IE
215-
.append(responseText.replace(rscript, ""))
216-
217-
// Locate the specified elements
218-
.find(selector) :
219-
220-
// If not, just inject the full result
221-
responseText );
222-
}
196+
data: params
197+
}).done(function( responseText ) {
223198

224-
if ( callback ) {
225-
self.each( callback, [ responseText, status, jqXHR ] );
226-
}
199+
// See if a selector was specified
200+
self.html( selector ?
201+
202+
// Create a dummy div to hold the results
203+
jQuery("<div>")
204+
205+
// inject the contents of the document in, removing the scripts
206+
// to avoid any 'Permission Denied' errors in IE
207+
.append( responseText.replace( rscript, "" ) )
208+
209+
// Locate the specified elements
210+
.find( selector ) :
211+
212+
// If not, just inject the full result
213+
responseText );
214+
215+
}).always(function() {
216+
if ( callback ) {
217+
self.each( callback, arguments );
227218
}
228219
});
229220

0 commit comments

Comments
 (0)