How to execute event just after the $ ('div'). load (url)?

0

I want to know how to do $('#div-search').on('load', ()=>{alert('carregou');});

I have a div where there are several $('#div-search').load(url) in the code. however now I need to perform a series of validations after loading it.

I have tried the following tests:

$('#div-search').on('load', ()=>{alert('carregou');});
$('#div-search').on('ready', ()=>{alert('carregou');});
$('#div-search').ready( ()=>{alert('carregou');}); 

and none of them worked.

  

P:

) 1. Why do not I want to have to search all code for calls; > 2. Because it is not always fixed $ ('# div-search') , it is often a variable $ (selectorId) and, in addition be harder to find, I would have to do an IF;

    
asked by anonymous 07.02.2018 / 15:57

1 answer

2

You can use .ajaxComplete , which will run whenever an ajax request completes.

$( document ).ajaxComplete(function() {
  console.log('carregou')
});

If you need to differentiate requests, use the parameters passed to the callback. Every time an ajax request is complete, an event object, the XMLHttpRequest object, and the configuration object used to create the request are passed to the function that used in .ajaxComplete . For example, if you want to restrict callback to only respond to a specific URL:

$( document ).ajaxComplete(function( event, xhr, settings ) {
  if ( settings.url === "google.com" ) {
    console.log( "Carregou. O resultado é" + xhr.responseText );
  }
});

Note :

  • As of version 1.9 of jQuery, the method call .ajaxComplete() needs to be made from document .

  • If $.ajax() or $.ajaxSetup() is called with the false value assigned to the global option, the .ajaxComplete() method will not be triggered.

07.02.2018 / 16:50