Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Javascript after ajax response has been rendered

I want to execute a piece of javascript after the ajax response has been rendered. The javascript function is being generated dynamically during the ajax request, and is in the ajax response. 'complete' and 'success' events to not do the job. I inspected the ajax request in Firebug console and response hasn't been rendered when the complete callback executes.

Does not work:

      function reloadForm() {
        jQuery.ajax({
          url: "<generate_form_url>",
          type: "GET",
          complete: custom_function_with_js_in_response()
        });
      };

ajaxComplete does the job, but it executes for all the ajax calls on the page. I want to avoid that. Is there a possible solution?

$('#link_form').ajaxComplete(function() {
  custom_function_with_js_in_response();
});
like image 962
amit_saxena Avatar asked Sep 24 '12 09:09

amit_saxena


1 Answers

you can also use $.ajax(..).done( do_things_here() );

$(document).ready(function() {

    $('#obj').click(function() {

    $.ajax({
        url: "<url>"
    }).done(function() {
      do_something_here();
    });
});

});

or is there another way

$(document).ready(function() {

    $('#obj').click(function() {

    $.ajax({
        url: "<url>",
        success: function(data){
          do_something_with(data);
        }
    })
});

});

Please, utilize this engine for share your problem and try solutions. Its very efficient.

http://jsfiddle.net/qTDAv/7/ (PS: this contains a sample to try)

Hope to help

like image 161
Bruno Guerra Avatar answered Sep 20 '22 11:09

Bruno Guerra