Hey. I'm busy with an AJAX script for filtering out usernames when typing a username into a text box. What happens is that when you start typing in a username into the text box, it takes the characters in the box and matches it with what is in the database and then returns the results.
But because Ajax isn't always the fastest, especially not on free servers, I want to add a loading image. I get it right to hide and show the image (with what I believe one would call jQuery). However, when I try and combine everything it does not work. I know where the problem is, but I don't know how to fix it.
In the onload of the image, I call a JS function to hide the image. When someone enters something in the textbox, onkeyup calls an AJAX functions. First, I show the image and then the ajax should happen, and when it is done, the loading image should be hidden again. But what happens is the image is hidden immediately, even if the AJAX is not done loading.
Does anyone know how I can check for when the Ajax is done loading, so I can wait before hiding the image?
Thank you
Relevant code:
But because Ajax isn't always the fastest, especially not on free servers, I want to add a loading image. I get it right to hide and show the image (with what I believe one would call jQuery). However, when I try and combine everything it does not work. I know where the problem is, but I don't know how to fix it.
In the onload of the image, I call a JS function to hide the image. When someone enters something in the textbox, onkeyup calls an AJAX functions. First, I show the image and then the ajax should happen, and when it is done, the loading image should be hidden again. But what happens is the image is hidden immediately, even if the AJAX is not done loading.
Does anyone know how I can check for when the Ajax is done loading, so I can wait before hiding the image?
Thank you
Relevant code:
Code:
function usernameSearch(str)
{
if (str.length==0)
{
document.getElementById("nameSearch").innerHTML="";
document.getElementById("nameSearch").style.border = "0px";
return;
}
else
{
$("#load").show(); //loading image show
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("nameSearch").style.border = "1px solid #A5ACB2";
document.getElementById("nameSearch").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getUsername.php?q="+str,true);
xmlhttp.send();
$("#load").hide(); //loading image hide (a wait is needed here)
}