I propose instead:

function ajax(u,s,t) {
    query = '';
    if (typeof s == "string") {
        d = jQuery(s).serialize();
        if(d){ query = d; }
    } else {
        pcs = [];switch (th
        if (s != null && s != undefined) for(i=0; i<s.length; i++) {
            q = jQuery("[name="+s[i]+"]").serialize();
            if(q){pcs.push(q);}
        }
        if (pcs.length>0){query = pcs.join("&");}
    }
*    jQuery.ajax({type: "POST",
                      url: u,
                      data: query,
                      success: function(msg) { 
                                         if(t) { 
                                           if(t==':eval') {
                                               eval(msg);
                                           } else if(typeof t == 'string') {
*
*                                              jQuery("#"+t)**.html(msg); *
*                                           } else {*
*                                              t(msg); *
*                                           }
                                        } 
    });*
}


On Friday, 2 March 2012 16:50:22 UTC-6, rochacbruno wrote:
>
> Hi,
>
> I would like to propose a change in the ajax function
>
> The ajax function when receiving a target, by default fires the .html() 
> function and replace all the contents of the target with the returning data.
>
> I always use .append() or .prepend() in the place of .html() so I end up 
> copuing ajax to append_ajax and prepend_ajax
>
>
> it can be done in this way:
>
> function ajax(u,s,t,*m*) {
>    * if (!m){var m = "html"};*
>     query = '';
>     if (typeof s == "string") {
>         d = jQuery(s).serialize();
>         if(d){ query = d; }
>     } else {
>         pcs = [];switch (th
>         if (s != null && s != undefined) for(i=0; i<s.length; i++) {
>             q = jQuery("[name="+s[i]+"]").serialize();
>             if(q){pcs.push(q);}
>         }
>         if (pcs.length>0){query = pcs.join("&");}
>     }
> *    jQuery.ajax({type: "POST",
>                       url: u,
>                       data: query,
>                       success: function(msg) { 
>                                          if(t) { 
>                                            if(t==':eval') {
>                                                eval(msg);
>                                            } else {
>                                               target = jQuery("#" + t);
>                                               switch(m){
>                                                   case "append":
>                                                       target.append(msg);
>                                                       break;
>                                                  case "prepend":
>                                                       target.prepend(msg);
>                                                       break;
>                                                  default:
>                                                       target.html(msg); 
>                                            } 
>                                         } 
>     });*
> }
>
>
> So we can use
>
> ajax(url, [], 'target', 'append'
>
>
> I dont know any other way of doing in JavaScript, but if someone knows how 
> to have dynamic functions in JS.
>
>  
> -- 
>
> Bruno Rocha
> [http://rochacbruno.com.br]
>
>

Reply via email to