ajax读取 一程序
里面有大片 mysql 和一个模板有的时候读取很慢 造成读取不完全 使得加载进来的页面混乱有没有什么办法能够判断ajax读取完毕后在输出到浏览器 如果没有全部读取完毕则不加载

解决方案 »

  1.   

    onreadystatechange指定当readyState属性改变时的事件处理句柄
    语法oXMLHttpRequest.onreadystatechange = funcMyHandler;Example如下的例子演示当XMLHTTPRequest对象的readyState属性改变时调用HandleStateChange函数,当数据接收完毕后(readystate == 4)此页面上的一个按钮将被激活var xmlhttp=null;
    function PostOrder(xmldoc)
    {
     var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.5.0");
     xmlhttp.Open("POST", "http://myserver/orders/processorder.asp", false); 
     xmlhttp.onreadystatechange= HandleStateChange;
     xmlhttp.Send(xmldoc);
     myButton.disabled = true;
    }
    function HandleStateChange()
    {
     if (xmlhttp.readyState == 4)
     {
       myButton.disabled = false;
       alert("Result = " + xmlhttp.responseXML.xml);
     }
    }
      

  2.   


    function creatXHR(){
    if (window.ActiveXObject) return new window.ActiveXObject('Microsoft.XMLHTTP');
    else if (window.XMLHttpRequest) return new window.XMLHttpRequest();
    else return null;
    }var xhr = creatXHR();
    xhr.open('GET', 'xml.xml');
    xhr.onreadystatechange = function(){
    if (xhr.readyState == 4 && xhr.status == 200) {
    // 这里是最后正确完成后执行的
    alert(xhr.responseText);
    }
    }
    xhr.send(null);
      

  3.   

    readyState == 4 && readyState  == 200
      

  4.   

    用2楼的方法吧,但我觉得应该进一步处理下.
               if(xmlhttp.readyState == 4)   //状态可用
                {     if(xmlhttp.status == 200)
                    {   
                        backfun(xmlhttp.responseText);//返回文本
                    }
                    else
                    {   alert("错误代码:"+xmlhttp.status);
                        backfun(null);
                    }
               }