There is a global event on my site which fires on every ajax call.
$(document).ajaxStart(function (event, xhr, settings) {
ShowLoader();
});
There are many ajax functionalities across the website and hence a global method.
And there is this new news feed feature on my site, where a user can click the button to see feed. This service is provided by a third party, who have asked me to place a script and add btn-NewsFeed
class to the button, like this:
<script type="text/javascript" src="url to a third party javascript"></script>
<a class="btn btn-NewsFeed">
Get News Feed
</a>
The anchor click makes an ajax call, but this method is in an external file which I cannot modify.
However, I can add another class & write a click event for this anchor tag on my site.
Is there a way, to just stop the global event for this anchor click and still not disturb its functionality.
2
Answers
You can do this in your global eventhandler function, something like this:
edit
This won’t work since this is not an
.on('click'), ...
handler where you have the element. So you have to do something different.You can set
global
to false, then this method will not fire, as per the documentation (you need to scroll down to “Additional Notes”:Edit: Further explanation necessary. Read the documentation of jQuery.ajax. Here you see the parameter
global
which is set to true by default. Set this one to false to not trigger.ajaxStart()
You can use
event.target.activeElement
to find which element currently has focus and based on that check if that element also have the classbtn-NewsFeed
. If true, simply return from theajaxStart
method, so thatShowLoader()
function in never called, else continue with it.