2011-05-11 119 views
0

我一直在試圖讓一些AJAX代碼在FireFox中正常運行以在IE中運行。 雖然我在腳本中更新一些表時遇到了一些麻煩。我見過很多其他人也有類似的問題,但他們發現的解決方案都沒有爲我工作。第一次出現問題就行了將Firefox轉換爲IE瀏覽器javascript

qe3Table.innerHTML = 
    "<tr>\n" + 
    " <th>Name</th>\n" + 
    " <th>Status</th>\n" + 
    " <th>View Status</th>\n" + 
    "</tr>\n"; 

當我得到我敢肯定,我的所有其他錯誤是的錯誤「‘空’爲空或不是對象」

與此類型相同,我的AJAX腳本和一些伴隨的javascript如下所示。

<script type="text/javascript"> 
<!-- 
//obtains the box address for a QE3 on the system for the given index 
function getQE3BoxAddressHash(index) 
{ 
    var retVal = 0x00000100; //initial value for QE3 boxes 
    retVal |= (index & 0x000000FF); 
    return retVal; 
} 

//obtains the box address for a QED on the system for the given index 
function getQEDBoxAddressHash(index) 
{ 
    var retVal = 0x00001300; //initial value for QED boxes 
    retVal |= ((index & 0x0000000F) << 4); 
    retVal |= ((index & 0x000000F0) >> 4); 
    return retVal; 
} 
--> 
</script> 
<script type="text/javascript"> 
<!-- 
var textSocket; 
function fillTables() 
{ 
    if(textSocket.readyState != 4) 
     return; 
    var qe3Table = document.getElementById("QE3_TABLE"); 
    var qedTable = document.getElementById("QED_TABLE"); 

    var rawData = textSocket.responseText.split("::::"); 

    var qe3Data = new Array(); 
    var qedData = new Array(); 

    var qe3Index = 0; 
    var qedIndex = 0; 


    for(var item in rawData) 
    { 
     if(rawData[item].indexOf("QA") != -1) 
     { 
      qe3Data[qe3Index++] = rawData[item]; 
     } 
     else if(rawData[item].indexOf("QED") != -1) 
     { 
      qedData[qedIndex++] = rawData[item]; 
     } 
    } 


    qe3Table.innerHTML = 
    "<tr>\n" + 
    " <th>Name</th>\n" + 
    " <th>Status</th>\n" + 
    " <th>View Status</th>\n" + 
    "</tr>\n"; 
    qedTable.innerHTML = 
    "<tr>\n" + 
    " <th>Name</th>\n" + 
    " <th>Status</th>\n" + 
    " <th>View Status</th>\n" + 
    "</tr>\n"; 

    for(var value in qe3Data) 
    { 
     var components = qe3Data[value].split("-"); 
     if(components.length != 3) 
      continue; 
     qe3Table.innerHTML += 
     "<tr>\n" + 
     " <td>" + components[0] + "-" + components[1] +"</td>\n" + 
     " <td>" + 
     ((components[2].toUpperCase() === "ONLINE")? 
       "<font color=\"green\"><b>ONLINE</b></font>": 
       "<font color=\"red\"><b>OFFLINE</b></font>")+ 
     "</td>\n" + 
     " <td>\n <input type=\"button\" onclick=\"window.location='system_status.php?boxAddress=" + getQE3BoxAddressHash(value).toString(16) + "'\" value='View Status for " + components[0] + "-" + components[1] +"'></input> </td>\n" + 
     "</tr>\n"; 
    } 
    for(var value in qedData) 
    { 
     var components = qedData[value].split("-"); 
     if(components.length != 3) 
      continue; 
     qedTable.innerHTML += 
     "<tr>\n" + 
     " <td>" + components[0] + "-" + components[1] +"</td>\n" + 
     " <td>" + 
     ((components[2].toUpperCase() === "ONLINE")? 
       "<font color=\"green\"><b>ONLINE</b></font>": 
       "<font color=\"red\"><b>OFFLINE</b></font>")+ 
     "</td>\n" + 
     " <td>\n <input type=\"button\" onclick=\"window.location='system_status.php?boxAddress=" + getQEDBoxAddressHash(value).toString(16) + "'\" value='View Status for " + components[0] + "-" + components[1] +"'></input> </td>\n" + 
     "</tr>\n"; 
    } 
} 

function initAjax() 
{ 
    try 
    { 
     // Opera 8.0+, Firefox, Safari 
     textSocket = new XMLHttpRequest(); 
    } 
    catch (e) 
    { 
     // Internet Explorer Browsers 
     try 
     { 
      textSocket = new ActiveXObject("Msxml2.XMLHTTP"); 
     } 
     catch (e) 
     { 
      try 
      { 
       textSocket = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      catch (e) 
      { 
       // Something went wrong 
       alert("A browser error occurred."); 
       return false; 
      } 
     } 
    } 

    textSocket.onreadystatechange=fillTables 
} 

function reloadTables() 
{ 
    textSocket.open("GET","ajax_scripts/get_connected_boxes.php",true); 
    textSocket.send(null); 
} 

function init() 
{ 
    initAjax(); 
    reloadTables(); 
} 

window.onload=init(); 
--> 
</script> 
+0

將其轉換爲jQuery(或選擇你喜歡的庫)。我敢打賭它會「修復自己」。 'window.onload = init()'是公然錯誤的,順便說一句。 – 2011-05-11 23:51:12

+0

只是要確保..在'textSocket.onreadystatechange = fillTables'的末尾添加';' – 2011-05-12 00:05:45

回答

2

這個問題可能是用:

var qe3Table = document.getElementById("QE3_TABLE"); 

如果你裝體前運行此腳本,將不存在。檢查該變量是否有任何內容。

0

我試了兩個人的修復,但他們似乎沒有幫助。最後,我轉換形式的所有呼叫:

qe3TableNew.innerHTML = ("<tr>\n" +" <th>Name</th>\n" +" <th>Status</th>\n" +" <th>View Status</th>\n" +"</tr>\n"); 

var row; 
    var cell; 
    var text; 
    var font; 
    row = document.createElement("tr"); 
    qe3TableNew.appendChild(row); 
    cell = document.createElement("th"); 
    row.appendChild(cell); 
    text = document.createTextNode("Name"); 
    cell.appendChild(text); 
    cell = document.createElement("th"); 
    row.appendChild(cell); 
    text = document.createTextNode("Status"); 
    cell.appendChild(text); 
    cell = document.createElement("th"); 
    row.appendChild(cell); 
    text = document.createTextNode("View Status"); 
    cell.appendChild(text); 

這似乎解決它,所以我相信這與IE的無法處理的變化innerHTML來辦。

感謝您的幫助球員。

0

至少一個問題(這可能產生上述症狀)是行:

window.onload=init(); 

提示:()操作者立即執行的功能和計算結果爲返回值。這反過來可能允許XHR處理程序(在某些情況下)在DOM未準備好時觸發。

快樂編碼。

相關問題