jQuery.fn.load optimization

This commit is contained in:
Oleg 2012-05-01 14:47:14 -04:00 committed by Rick Waldron waldron.rick@gmail.com
parent 6bed348a00
commit 95291b44e6

View File

@ -158,25 +158,25 @@ jQuery.fn.extend({
load: function( url, params, callback ) { load: function( url, params, callback ) {
if ( typeof url !== "string" && _load ) { if ( typeof url !== "string" && _load ) {
return _load.apply( this, arguments ); return _load.apply( this, arguments );
}
// Don't do a request if no elements are being requested // Don't do a request if no elements are being requested
} else if ( !this.length ) { if ( !this.length ) {
return this; return this;
} }
var off = url.indexOf( " " ); var selector, type,
self = this,
off = url.indexOf(" ");
if ( off >= 0 ) { if ( off >= 0 ) {
var selector = url.slice( off, url.length ); selector = url.slice( off, url.length );
url = url.slice( 0, off ); url = url.slice( 0, off );
} }
// Default to a GET request
var type = "GET";
// If the second parameter was provided
if ( params ) {
// If it's a function // If it's a function
if ( jQuery.isFunction( params ) ) { if ( jQuery.isFunction( params ) ) {
// We assume that it's the callback // We assume that it's the callback
callback = params; callback = params;
params = undefined; params = undefined;
@ -185,31 +185,23 @@ jQuery.fn.extend({
} else if ( typeof params === "object" ) { } else if ( typeof params === "object" ) {
type = "POST"; type = "POST";
} }
}
var self = this;
// Request the remote document // Request the remote document
jQuery.ajax({ jQuery.ajax({
url: url, url: url,
// if "type" variable is undefined, then "GET" method will be used
type: type, type: type,
dataType: "html", dataType: "html",
data: params, data: params
// Complete callback (responseText is used internally) }).done(function( responseText ) {
complete: function( jqXHR, status, responseText ) {
// Store the response as specified by the jqXHR object
responseText = jqXHR.responseText;
// If successful, inject the HTML into all the matched elements
if ( jqXHR.isResolved() ) {
// #4825: Get the actual response in case
// a dataFilter is present in ajaxSettings
jqXHR.done(function( r ) {
responseText = r;
});
// See if a selector was specified // See if a selector was specified
self.html( selector ? self.html( selector ?
// Create a dummy div to hold the results // Create a dummy div to hold the results
jQuery("<div>") jQuery("<div>")
// inject the contents of the document in, removing the scripts // inject the contents of the document in, removing the scripts
// to avoid any 'Permission Denied' errors in IE // to avoid any 'Permission Denied' errors in IE
.append( responseText.replace( rscript, "" ) ) .append( responseText.replace( rscript, "" ) )
@ -219,11 +211,10 @@ jQuery.fn.extend({
// If not, just inject the full result // If not, just inject the full result
responseText ); responseText );
}
}).always(function() {
if ( callback ) { if ( callback ) {
self.each( callback, [ responseText, status, jqXHR ] ); self.each( callback, arguments );
}
} }
}); });