SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    jQuery function missing parameter

    This is the Ajax GET function:
    jQuery.get( url, [ data ], [ callback(data, textStatus, XMLHttpRequest) ], [ dataType ] )

    However, I see examples of code like this where the first and third parameters are provided but the second is missing:
    $.get('ajax/test.html', function(data) {
    $('.result').html(data);
    alert('Load was performed.');
    });

    I'm familiar with providing a '' or NULL value when a parameter is optional, so how does jQuery/javascript handle missing parameters?

  2. #2
    SitePoint Addict Zarin Denatrose's Avatar
    Join Date
    Jan 2009
    Location
    Surrey BC, Canada
    Posts
    309
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    From the jQuery source
    Code JavaScript:
    jQuery.extend({
        get: function( url, data, callback, type ) {
            // shift arguments if data argument was omited
            if ( jQuery.isFunction( data ) ) {
                type = type || callback;
                callback = data;
                data = null;
            }
     
            return jQuery.ajax({
                type: "GET",
                url: url,
                data: data,
                success: callback,
                dataType: type
            });
        },

    it checks to see if the data parameter is a function, in which case it sets type to type or callback (first defined value.), callback to data, and data to null then calls jQuery.ajax() with everything where it should be. That keeps developers from being forced to add empty strings and nulls, needlessly complicating their own code.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •