Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 15th, 2012  |  syntax: None  |  size: 2.77 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. passing optional paramaters in javascript/jquery
  2. .load( url [, data] [, complete(responseText, textStatus, XMLHttpRequest)] );
  3.        
  4. $('#result').load('ajax/test.html', function(){
  5.     alert('Load was performed.');
  6. }
  7.        
  8. if ( params ) {
  9.     // If it's a function
  10.         if ( jQuery.isFunction( params ) ) {
  11.         // We assume that it's the callback
  12.         callback = params;
  13.         params = null;
  14.     // Otherwise, build a param string
  15.     } else if ( typeof params === "object" ) {
  16.         params = jQuery.param( params, jQuery.ajaxSettings.traditional );
  17.         type = "POST";
  18.     }
  19. }
  20.        
  21. isFunction: function( obj ) {
  22.     return jQuery.type(obj) === "function";
  23. },
  24. type: function( obj ) {
  25.     return obj == null ?
  26.         String( obj ) :
  27.         class2type[ toString.call(obj) ] || "object";
  28. },
  29.        
  30. class2type[ "[object Function]" ] = "function";
  31.        
  32. load: function( url, params, callback ) {
  33.  
  34.     if (typeof url !== "string" && _load) {
  35.         // My comment: the function.apply ia an
  36.         // alternative way to call a javascript function,
  37.         // where the number of parameter in `arguments`
  38.         // couldn't be determined
  39.         // the `arguments` is a special javascript variable.
  40.         // It's an array containing the parameters within
  41.         // the context of a function
  42.         return _load.apply(this, arguments);
  43.  
  44.     // Don't do a request if no elements are being requested        
  45.     } else if ( !this.length ) {            
  46.         return this;        
  47.     }      
  48.  
  49.     var off = url.indexOf( " " );      
  50.     if ( off >= 0 ) {          
  51.         var selector = url.slice( off, url.length );            
  52.         url = url.slice( 0, off );      
  53.     }      
  54.  
  55.     // Default to a GET request    
  56.  
  57.     var type = "GET";      
  58.     // If the second parameter was provided    
  59.     if ( params ) {        
  60.         // If it's a function          
  61.         if ( jQuery.isFunction( params ) ) {
  62.             // We assume that it's the callback            
  63.             callback = params;              
  64.             params = undefined;        
  65.             // Otherwise, build a param string          
  66.         } else if ( typeof params === "object" ) {              
  67.             params = jQuery.param( params, jQuery.ajaxSettings.traditional );              
  68.             type = "POST";          
  69.         }      
  70.     }
  71.  
  72.  .... other code
  73.        
  74. // If the second parameter was provided
  75.     if ( params ) {
  76.         // If it's a function
  77.         if ( jQuery.isFunction( params ) ) {
  78.             // We assume that it's the callback
  79.             callback = params;
  80.             params = undefined;
  81.  
  82.         // Otherwise, build a param string
  83.         } else if ( typeof params === "object" ) {
  84.             params = jQuery.param( params, jQuery.ajaxSettings.traditional );
  85.             type = "POST";
  86.         }
  87.     }