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 ){
$("#load").hide(); //loading image hide
if(xmlhttp.status==200)
{
document.getElementById("nameSearch").style.border = "1px solid #A5ACB2";
document.getElementById("nameSearch").innerHTML=xmlhttp.responseText;
} else {
;// code, if any, to handle 404, 301, 500, etc server errors
}
}
}
xmlhttp.open("GET","getUsername.php?q="+str,true);
xmlhttp.send();
}
Move the hide code inside the onreadystatechange handler when you get a response. You have to separate it from the 200 (success) code, otherwise 500/404/301/etc errors will leave the image.