Global Callback for Ajax Calls in JQuery

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

EDIT:

The solution provided by Nakul (using the ajaxSuccess global event) is almost perfect. However, when using the load function, I have a problem. The ajaxSuccess event occurs after the request is completed, but before any manipulations with the DOM. I would like to execute some code after changing the DOM. My temporary solution is to use setTimeout and wait a few milliseconds, but I don't think it would be reliable enough for me.

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

EDIT 2:

I managed to solve the second problem using the ajaxComplete event instead of ajaxSuccess.

+7
jquery ajax callback
source share
2 answers
+4
source share

Use ajaxSuccess and maybe want to run the code when you click the 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'); }); }); 
+1
source share

All Articles