本文實例講述了PHP實現簡單ajax Loading加載功能。分享給大家供大家參考,具體如下:
var xmlHttp;function createXmlHttpReq() {  if(window.ActiveXObject) {    xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');  } else if(window.XMLHttpRequest) {    xmlHttp = new XMLHttpRequest();  }}function funMy(url) {  createXmlHttpReq();  try {    xmlHttp.onreadystatechange = cb;//一定要在open()前,下邊會有說明。在此處犯錯了    xmlHttp.open("GET","for.php?id="+url,true);    xmlHttp.send(null);  } catch(e) {    alert("您訪問的資源不存在");  }}//回調函數function cb() {  if(xmlHttp.readyState==1) {    alert("1-------------->");    //在Google Chrome 瀏覽器里不顯示loading圖片,三秒后顯示內容,問題已解決,下邊有說明    document.getElementById('ajax').innerHTML = "<img src=loading2.gif>";    //document.getElementById('ajax').innerHTML = "Loading......";  }  if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {      var data = xmlHttp.responseText;      document.getElementById('ajax').innerHTML = data;  }}測試的時候,被 Chrome 給絆著了。請看下邊解釋:
這樣寫本來就不會接到.readyState==1的回應
因為1是表示已經調用.open()完成
但是.open()在.onreadystatechange事件前就被調用了,所以你應該不可能接到.readyState==1的回應
	故,想接到.readyState==1 =>.onreadystatechange必須在.open()之前
	那么為什么有時候接到呢?
因為你使用同一個全局變量...在連續操作時有可能會因為一個xhr請求還在等候php而又將它再次初始化引發
應該先決定數據的處理方式onreadystatechange,在送出要處理的數據open()
希望本文所述對大家PHP程序設計有所幫助。
新聞熱點
疑難解答
圖片精選