2011-05-11 118 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未准备好时触发。

快乐编码。

相关问题