1

I have a webpage where ajax calls trigger an overlay using jQuery like so:

$(document).on({ ajaxStart: function() { $("body").addClass("loading"); }, ajaxStop: function() { $("body").removeClass("loading"); } }); 

This is fine for me, however there are a couple of ajax calls in the page which are background jobs and I do not want the loading message to be shown for those ajax call.

Is there a way to achieve this?

2 Answers 2

2

Absolutely. You need to set the global to false in the $.ajax() method only where you don't want to apply the .ajaxStart()/.ajaxStop() events to be fired:

$.ajax({ url:'', global:false, //<----add this success:fn }) 

Note:

You can use it in $.ajax() only.


From the docs:

global (default: true)

Type: Boolean

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

Sign up to request clarification or add additional context in comments.

2 Comments

thanks this helps...my functions are $.post, need to check if it will work there.
@UndefinedVariable you can see i have suggested you to use $.ajax(). Although i have never tried to use global in $.post() etc. shorthand ajax methods.
0

You can use a flag variable defined in window scope, set it to false when you do not want to ajaxStart / ajaxEnd effect. Otherwise it should be true

$(document).on({ ajaxStart: function() { if(ajaxFlag){ $("body").addClass("loading"); ajaxFlag=true; }, ajaxStop: function() { if(ajaxFlag) $("body").removeClass("loading"); } }); 

2 Comments

if i set it to false, then next place would I need to set it to true again? if I do then it will be a hassle..
You can set that true after add class in the condition

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.