views:

92

answers:

2

Is it possible to have a global event handler for ajax requests that automatically gets called when any ajax request returns ?

I'm interested in this because I'm making a greasemonkey script for an ajax site. In previous scripts I either ran the main function every few seconds or overwrote parts of the site's javascript, both things that are messy.

+1  A: 

Not with regular XMLHttpRequest. Some libraries like jQuery provide wrappers (e.g. ajaxComplete) for this. They will only fire for ajax requests that also use the wrapper (e.g. jQuery.ajax).

Matthew Flaschen
A: 

using jQuery I do this.

$(document).ready(function() {
    $("#contentLoading").hide();

    $("#contentLoading").ajaxSend(function(r,s){
        $(this).show();
        $("#ready").hide();
    });

    $("#contentLoading").ajaxStop(function(r,s){
        $(this).hide();
        $("#ready").show();
    });

Instead of hiding and showing things you could have it run a generic function.

Gutzofter