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.
精彩评论