2

According to jQuery load() method api:

.load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] )
  1. 1st parameter is url
  2. 2nd parameter is map or string that is sent to the server
  3. 3rd parameter is callback function.

With the working example below

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

it supplies arguments of 'url' and 'callback function', [data] argument is skipped.

Shouldn't the example code treat the callback function as [data] argument (2nd parameter) ? Because of the order that parameters defined in the API . By following the API, 1st is url, 2nd is data, 3rd is callback.

I don't get why the code would work. Very confused.

5 Answers 5

5

It is very clearly written in the jQuery source code.

https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js

Search for load: function( url, params, callback )

It checks for the params (second parameter) and if it exist, It will call the isFunction method which internally check the type of the argument and return true if it is a function. The rest you know....

enter image description here

This is how isFunction looks like

enter image description here

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

Comments

2

No, It inspects the datatype of the parameters. If it finds a function as the second parameter then it uses it as a callback.

The position and order of the parameters has been thought out with the typical use-cases in mind and in stead of having to give a null value to skip a parameter .load('url', null, null, function() {}); you just imagine that the parameters "shift" places when skipped.

This applies to a lot of functions not just .load.

3 Comments

thx, the datatype inspection is jQuery's feature ro javascript's feature? With other language such as PHP, we always need to follow the order.
Not true, you can do the same in PHP too, an it's a JS feature not a jquery feature.
The same type inspection can be used in PHP to obtain similar results. When you define the method you don't name the parameters in the function header, and you fetch all supplied parameters from func_get_args() you inspect their types and decide what you want to do with them.
1

The square brackets ([]) around a parameter in the documentation indicate that it is optional. So your example is perfectly valid according to said documentation.

10 Comments

I understand it is optional. But shouldn't the extra augments follow the parameter order defined?
They do. Since data is optional, it is perfectly valid to supply a callback function as the second argument.
then why load() won't treat "function() {alert('Load was performed.'); })" as [data] argument, as its position is in 2nd
Andrew, in most other programming languages the convention for skipping optional arguments is to use the default value of that parameter or if it's the last parameter to not include it at all. The jQuery convention is helpful but I can understand why this would be considered unintuitive at first.
He knows that it is valid to ignore it but he is not sure how jquery understands whether 2nd argument is for params or callback
|
1

Checkout jquery's source file ajax.js in Github: https://github.com/jquery/jquery/blob/master/src/ajax.js#L178

Here it checks whether second argument is function. If yes, it takes it as the callback and params as undefined.

2 Comments

thanks. great find, is the checking of argument datatype also applying to other jQuery method?
Once it determines which is params and which is callback, it calls jQuery.ajax method to provide parameters correctly. As you know, .load is just a shorthand method for jQuery.ajax
0

The brackets in the specification means that the parameters are optional, so you can use any of these forms:

.load(url, data, complete)
.load(url, data)
.load(url, complete)
.load(url)

The method will figure out if the second parameter is a callback function or a data object/string depending on the data type.

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.