2

The JQuery .load() definition follows:

.load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] );

The data and complete() arguments are optional. Right? But then, how we can call this:

$('#result').load('ajax/test.html', function(){
    alert('Load was performed.');
}

How jquery ignores the 2nd argument and knows that we provided the 1st and the 3d argument? And what if the 3d argument is not a function?

1
  • 1
    I would assume that jQuery knows that the data parameter should not be a function, and inspects the type of the argument. If it is a function, then it assumes that it must be the completion handler, because that is the only parameter that should be a function. Commented Dec 14, 2011 at 7:46

3 Answers 3

2

See the source code.

load: function( url, params, callback ) {

    if (typeof url !== "string" && _load) {
        // My comment: the function.apply ia an 
        // alternative way to call a javascript function, 
        // where the number of parameter in `arguments` 
        // couldn't be determined
        // the `arguments` is a special javascript variable. 
        // It's an array containing the parameters within 
        // the context of a function
        return _load.apply(this, arguments);

    // Don't do a request if no elements are being requested        
    } else if ( !this.length ) {            
        return this;        
    }       

    var off = url.indexOf( " " );       
    if ( off >= 0 ) {           
        var selector = url.slice( off, url.length );            
        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 ( jQuery.isFunction( params ) ) {
            // We assume that it's the callback             
            callback = params;              
            params = undefined;         
            // Otherwise, build a param string          
        } else if ( typeof params === "object" ) {              
            params = jQuery.param( params, jQuery.ajaxSettings.traditional );               
            type = "POST";          
        }       
    }

 .... other code
Sign up to request clarification or add additional context in comments.

Comments

1

If load is called with two arguments or more, jQuery checks to see if the second argument is a function or an object. If it's a function it's called when the ajax call is completed. Otherwise it's used as the params passed with the ajax call.

Relevant parts from the source:

if ( params ) {
    // If it's a function
        if ( jQuery.isFunction( params ) ) {
        // We assume that it's the callback
        callback = params;
        params = null;
    // Otherwise, build a param string
    } else if ( typeof params === "object" ) {
        params = jQuery.param( params, jQuery.ajaxSettings.traditional );
        type = "POST";
    }
}

Where params is the second argument to load.

isFunction is the result of the following functions:

isFunction: function( obj ) {
    return jQuery.type(obj) === "function";
},
type: function( obj ) {
    return obj == null ?
        String( obj ) :
        class2type[ toString.call(obj) ] || "object";
},

where class2type is an assosiative array containing, among other things, this element:

class2type[ "[object Function]" ] = "function";

Comments

1

Let's see the source code, so you will see if the second parameter is a function, then pass the second parameter params to the third parameter callback and set the second parameter params to undefined.

    // If the second parameter was provided
    if ( params ) {
        // If it's a function
        if ( jQuery.isFunction( params ) ) {
            // We assume that it's the callback
            callback = params;
            params = undefined;

        // Otherwise, build a param string
        } else if ( typeof params === "object" ) {
            params = jQuery.param( params, jQuery.ajaxSettings.traditional );
            type = "POST";
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.