views:

118

answers:

2

i'm having a ajax request on the page.

is their any way to find when a ajax is requested on the page.

in jquery or javascript to find or initiate a function whn ajax request is called in a page.

+2  A: 

See ajaxStart and ajax events. Example:

 $("#loading").bind("ajaxSend", function(){
   $(this).show();
 }).bind("ajaxComplete", function(){
   $(this).hide();
 });
karim79
loading can be a form, its not working..
santhosh
@santhosh - can show what you've tried in the question? It's hard to picture what you're trying to do based on information you have given.
karim79
$(document).bind("ajaxSuccess", function() { Sys.Debug.trace(this);});for this its working, but its bind all ajax rquest. How can i filter to find particular ajax call.
santhosh
@santhosh - just use $.ajax's beforeSend, complete and success callbacks then :)
karim79
There only problem comes, i can't add anything $.ajax
santhosh
A: 

You can bind a function to a JQuery object to be completed when any AJAX call completes.

$('#status').ajaxComplete(function(event, request, settings) {
    // Do stuff here...
});

See the documentation.

If you want to set a complete function for a single AJAX call, use the low-level $.ajax() function and setting a function for the complete attribute.

$.ajax({
    // ...
    complete: function(request, settings) {
         // Do stuff here...
    },
    // ...
});

Note: the documentation seems to contradict itself in specifying the number of arguments to the complete() function. It may take some fiddling.

Brendan Berg