Load method has beforeSend?

2

I would like to know if in the .load () method of JQuery, you have some way to use beforeSend, just like we use direct in $ .ajax ();

Would you?

$.ajax({
        url: "https://www.blablabla.com.br",
        beforeSend: function(){
            $("body").html('<p>Carregando...</p>')
        },
        success: function(r){
            $("body").html(r);
        }
    });
});

I would like to do this part:

beforeSend: function(){
   $("body").html('<p>Carregando...</p>')
},

in method load() ?

    
asked by anonymous 30.10.2017 / 14:59

1 answer

1

No.

The jQuery.load() method, as well as jQuery.get() , is a simplification of > jQuery.ajax() .

What happens in the case of load() is that the server response will be inserted inside the element that is specified in the first parameter when the request is terminated.

You can see it in the source code of the function itself.

I just took the main part of the function (I removed the comments and formatted to make it easier to read).

jQuery.ajax({
    url: url,
    type: type || "GET",
    dataType: "html",
    data: params
}).done(function(responseText){
    response = arguments;

    self.html( selector ?
            jQuery( "<div>" ).append( jQuery.parseHTML( responseText ) ).find( selector ) :
            responseText );

}).always( callback && function( jqXHR, status ) {
    self.each( function() {
        callback.apply( this, response || [ jqXHR.responseText, status, jqXHR ] );
    });
});

View the full source on GitHub.

In documentation , you can see that the function receives three parameters:

  • url : The URL to make the request;
  • data : Object or string to send to the server in the request; and
  • complete : callback function to run when the request is complete.

If you want to use beforeSend it's best to use jQuery.ajax() .

    
30.10.2017 / 15:47