- passing optional paramaters in javascript/jquery
- .load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] );
- $('#result').load('ajax/test.html', function(){
- alert('Load was performed.');
- }
- 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";
- }
- }
- isFunction: function( obj ) {
- return jQuery.type(obj) === "function";
- },
- type: function( obj ) {
- return obj == null ?
- String( obj ) :
- class2type[ toString.call(obj) ] || "object";
- },
- class2type[ "[object Function]" ] = "function";
- 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
- // 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";
- }
- }