Reputation: 610
My Problem: I have got this AJAX call inside of my website that loads some content into a given DIV. While loading this content, a GIF loading animation gets inserted (via JavaScript), until AJAX returns the requested content.
Which works perfectly fine inside of Firefox, because FF loads the JavaScript in a linear order. Chrome and IE on the other hand do not seem to play that game. It seems that they're firing all AJAX first, then handling everything else.
Nice to know: The async on my AJAX call is FALSE. Yes I know, I should aim for the use of a TRUE async but in this case it just has to be that way.
My Goal: Since I know I can't do anything while my AJAX is loading, I would like to execute the line of code that inserts my animation BEFORE any AJAX gets called. I tried a setTimeout() but that just breaks the whole script.
Any suggestions?
Here's the code:
<div id="mycolumn">
<!-- Here: Loading Animation first, then AJAX responseText -->
</div>
JavaScript Function:
function myAjaxCall () {
// Initiate HTTP Request
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
// I want this to happen BEFORE any AJAX gets executed
document.getElementById("mycolumn").innerHTML="<img src='loading.gif'>";
// AJAX Request, gets fired by Chrome and IE before anything else,
// which prevents my img from being inserted before the response arrives
xmlhttp.open("GET","someURL",false);
xmlhttp.send(null);
// AJAX Response, replaces my Loading Animation
document.getElementById("mycolumn").innerHTML=xmlhttp.responseText;
}
Upvotes: 0
Views: 129