开发者

How can I catch all ajax requests in javascript?

开发者 https://www.devze.com 2022-12-28 17:23 出处:网络
Is it possible to have a global event handler for ajax requests that automatically gets called when any aj开发者_JAVA百科ax request returns ?

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

精彩评论

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

关注公众号