Local AjaxStart/Stop?
I have a global ajax start and stop. I am wondering though if this can be overridden by a local one as one of my ajax methods needs to do something different when ajax starts and stops
I tried like
$.ajax({
url: "test.html",
context: document.body,
success: function(){
$(t开发者_Python百科his).addClass("done");
},
ajaxStart: function()
{
alert('hi');
}
});
You can do this by setting global:false
in your jQuery.ajax call.
global (Boolean)
Default: trueWhether to trigger global Ajax event handlers for this request. The default is true. Set to false to prevent the global handlers like ajaxStart or ajaxStop from being triggered. This can be used to control various Ajax Events.
You can refer to another function (successHandler) or even to a function by name since all global functions actually become a method of the window object. For that last solution, all you need to do is a add a string variable to you public and local pages that contains the right function name.
function successhandler()
{
$(this).addClass("done");
}
function ajaxStart()
{ alert('hi');
}
$.ajax({
url: "test.html",
context: document.body,
success: successHandler,
ajaxStart: window['ajaxStart']
});
精彩评论