views:

190

answers:

2

Is it possible to create and attach a callback which would get called whenever an ajax request completes regardless of whether the call was made using $.ajax, $.post, load or any other function?

EDIT:

The solution given by Nakul (using ajaxSuccess global event) is almost perfect. However I have a problem when using this with the load function. The ajaxSuccess event is raised after the request has completed but before any DOM manipulation. I would like to execute some code after the DOM has been modified. My temporary solution is to use setTimeout and wait for a couple of milliseconds but I don't think it'd be reliable enough for me.

So another question is: how can you execute code after the DOM has been manipulated by the load function?

EDIT 2:

I've managed to solve the second issue by using ajaxComplete event instead of ajaxSuccess.

+1  A: 

You can use ajaxSuccess More here: http://www.slideshare.net/wildan.m/jquery-talk-to-server-with-ajax (after slide 47)

Nakul
This is almost what I was looking for, but I have another problem; I've updated the question.
Immortal
+1  A: 

Use the ajaxSuccess and maybe you want to run the code when you click a link?

HTML:

<a href="javascript:void(0);" class="runCode">Run code</a>

JS:

$(function() { // same as dom loaded
    $('a.runCode').click(function() {
        alert('whatever you want to do here');
    });
});
Mickel