Local AjaxStart / Stop?

I have a global start and stop ajax. I'm curious though if this can be overridden local, as one of my ajax methods should do something different when ajax starts and stops

I tried both

$.ajax({ url: "test.html", context: document.body, success: function(){ $(this).addClass("done"); }, ajaxStart: function() { alert('hi'); } }); 
+4
source share
2 answers

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 this last solution, all you have to do is add a string variable to your public and local pages containing the correct function name.

 function successhandler() { $(this).addClass("done"); } function ajaxStart() { alert('hi'); } $.ajax({ url: "test.html", context: document.body, success: successHandler, ajaxStart: window['ajaxStart'] }); 
+1
source

You can do this by setting global:false in your jQuery.ajax call.

global (Boolean)
Default: true

Whether to fire the global Ajax event handlers for this request. The default value is true. Set to false to prevent global handlers such as ajaxStart or ajaxStop from being fired. This can be used to manage various Ajax events.

+3
source

All Articles