I'm trying to display a loading image after an AJAX call has been made.
Currently I have the following Javascript code:
window.onload = function(){
var img = document.createElement("IMG");
img.src = "https://i.stack.imgur.com/FhHRx.gif";
function getCategories(){
$.ajax({
url: //url here,
dataType:'jsonp',
success: function(data){
//code here
},
statusCode: {
404: function() {
alert:('There was a problem with the server');
}
}
});
}
$(document).ajaxStart(function(){
alert('here');
var div = document.createElement("div");
div.id = "id1";
document.getElementById('id1').appendChild(img);
document.body.appendChild(div);
}).ajaxStop(function(){
var element = document.getElementById('id1');
element.parentNode.removeChild(element);
});
However, I am finding that the code is not even getting to the ajaxStart function as the alert is not appearing... I have tried other methods (eg: How to show loading spinner in jQuery?) to try and get this working but have not been successful as of yet...
Would anyone be able to help with this please?
Thank you.