最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery load() method parameters usage? - Stack Overflow

programmeradmin2浏览0评论

According to jQuery load() method api:

.load( url [, data] [, plete(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.

According to jQuery load() method api:

.load( url [, data] [, plete(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.

Share Improve this question asked May 28, 2012 at 15:43 gilzerogilzero 1,9124 gold badges20 silver badges28 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

It is very clearly written in the jQuery source code.

https://ajax.googleapis./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....

This is how isFunction looks like

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.

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

Checkout jquery's source file ajax.js in Github: https://github./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.

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

.load(url, data, plete)
.load(url, data)
.load(url, plete)
.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.

发布评论

评论列表(0)

  1. 暂无评论