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();
});
-
Why not use ajaxComplete? It may execute for every ajax calls but using a condition in it would fix the errors that may occur on "normal" requests. Or you could use a wrapper for such ajax calls that define the ajaxComplete method and inside it it removes itself once it is called...Salketer– Salketer2012年09月24日 09:57:19 +00:00Commented Sep 24, 2012 at 9:57
-
1Please put the code. Ideally if you are putting code in success/error method of ajax, it should work. However if code is present, we can figure out if you are doing any mistake.Kapil Sharma– Kapil Sharma2012年09月24日 09:57:43 +00:00Commented Sep 24, 2012 at 9:57
-
what JS framework do you use? Can yu put the code sample?Mat– Mat2012年09月24日 09:58:19 +00:00Commented Sep 24, 2012 at 9:58
-
@Salketer...didn't get the second part of your comment. Can you please elaborate. Added code, and I use jquery.amit_saxena– amit_saxena2012年09月24日 10:18:10 +00:00Commented Sep 24, 2012 at 10:18
4 Answers 4
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
1 Comment
Checking (and deferring call if needed) and executing the existence of the callback function might work:
// undefine the function before the AJAX call
// replace myFunc with the name of the function to be executed on complete()
myFunc = null;
$.ajax({
...
complete: function() {
runCompleteCallback(myFunc);
},
...
});
function runCompleteCallback(_func) {
if(typeof _func == 'function') {
return _func();
}
setTimeout(function() {
runCompleteCallback(_func);
}, 100);
}
4 Comments
runCompleteCallback function checks if the passed in function exists (which mean the AJAX response has already gone into the DOM) and executes it if it does. If it doesn't, it tries again after 100ms (change this duration as needed). In most cases, it will pass in the second run max (unless data-insertion into the DOM takes a long time, which is unlikely).myFunc = null; ) immediately before the AJAX call, so that runCompleteCallback thinks it does not exist and will wait till the new version gets pushed into the DOM.Can't help a lot without code. As an general example from JQuery ajax complete page
$('.log').ajaxComplete(function(e, xhr, settings) {
if (settings.url == 'ajax/test.html') {
$(this).text('Triggered ajaxComplete handler. The result is ' +
xhr.responseHTML);
}
});
In ajaxComplete, you can put decisions to filter the URL for which you want to write code.
1 Comment
Try to specify function name without () in ajax options:
function reloadForm() {
jQuery.ajax({
url: "<generate_form_url>",
type: "GET",
complete: custom_function_with_js_in_response
});
};