How to disable all AJAX requests in a page with jQuery and Javascript ?
ajax, javascript, jquery, web
Solution
If all of your ajax requests are being sent through jQuery ajax methods (including helper methods), you can do this with beforeSend.
window.ajaxEnabled = true;
$.ajaxSetup({
beforeSend: function(){
return window.ajaxEnabled;
}
});
$.post("http://www.google.com"); // throws an error
window.ajaxEnabled = false;
$.post("http://www.google.com"); // doesn't throw an error
http://jsfiddle.net/k2T95/3
And here's one that will block all, regardless of what javascript library is sending it, also based on a global flag. Doesn't affect XDomainRequest obj though
(function (xhr) {
var nativeSend = xhr.prototype.send;
window.ajaxEnabled = true;
xhr.prototype.send = function () {
if (window.ajaxEnabled) {
nativeSend.apply(this, arguments);
}
};
}(window.XMLHttpRequest || window.ActiveXObject));
http://jsfiddle.net/k2T95/4/
Problem
I have a page and I would like to disable all AJAX requests with jQuery. Do you have any ideas? And if it is possible? ``` if (false) { //disable all ajax requests } ```