开发者

How can I catch all ajax requests in javascript?

Is it possible to have a global event handler for ajax requests that automatically gets called when any aj开发者_JAVA百科ax 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.


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜