开发者

ajax button loading state

I made this little script with tutorials on internet. php function calls this javascript as many times as there are buttons (foreach), right now i have three. $value is the div name of specific button (buttons are stored in php array).

Everything works fine... except when i click through all the buttons fast, the loading gif remains without javascript changing the button status. The response, witch it gets from another php is the new button state and session variable change. Session variable gets changed but the div dosent.

So, heres where i need help, how can i make it so, when i click buttons fast, the div gets changed too?

my code

function load_javascripts() {

    foreach ($GLOBALS["VARIABLES"]["button_list"] as $key => $value) {
        $scripts .= "

        function run_alias_button_".$value."(str)
        {
        document.getElementById('aliasbu开发者_如何转开发tton_".$value."').innerHTML='<img src=ajax_loader.gif>';
        if (str=='')
          {
          document.getElementById('aliasbutton_".$value."').innerHTML='';
          return;
          } 
        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('aliasbutton_".$value."').innerHTML=xmlhttp.responseText;
            }
          }
        xmlhttp.open('GET','?leht=alias_logimine&alias=".$value."',true);
        xmlhttp.send();
        }
        ";
    }
    return $scripts;
}


Put var xmlhttp; at the very beginning of the function, making the variable explicitly local. Sometimes without that statement browsers may try to find a global variable with this name and readystate monitoring is shifted from one request to another.


Just a tip. Use the open function always before the onreadystatechange event. The way you using may works on firefox but IE doens't understand. LIke this:

xmlhttp.open('GET','?leht=alias_logimine&alias=".$value."',true);
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById('aliasbutton_".$value."').innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.send();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜