Does load method have beforeSend?

Asked

Viewed 347 times

2

I wonder, if in Jquery’s . load() method, there is some way to use beforeSend, just like we use it directly in $.ajax();

Would have?

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

I would have been able to do that part:

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

in the method load() ?

1 answer

1


Not.

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

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

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

I only took the main part of the function (I removed the comments and formatted it to be 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 ] );
    });
});

See the full source on Github.

In the documentation, it is possible to see that the function receives three parameters:

  • url: The URL to request;
  • data: Object or string to send to the server in the request; and
  • complete: Function of callback to be executed when the request is completed.

If you intend to use the beforeSend better use jQuery.ajax().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.