将FirefoxJavaScript转换为IE javascript
我一直在尝试让一些在FireFox中运行良好的AJAX代码在IE中运行。 不过,我在更新脚本中的一些表时遇到了一些问题。我见过很多其他人也有类似的问题,但他们找到的解决方案都不适合我。问题首先出现在线路上将FirefoxJavaScript转换为IE javascript,javascript,ajax,internet-explorer,firefox,Javascript,Ajax,Internet Explorer,Firefox,我一直在尝试让一些在FireFox中运行良好的AJAX代码在IE中运行。 不过,我在更新脚本中的一些表时遇到了一些问题。我见过很多其他人也有类似的问题,但他们找到的解决方案都不适合我。问题首先出现在线路上 qe3Table.innerHTML = "<tr>\n" + " <th>Name</th>\n" + " <th>Status</th>\n" + " <th>View S
qe3Table.innerHTML =
"<tr>\n" +
" <th>Name</th>\n" +
" <th>Status</th>\n" +
" <th>View Status</th>\n" +
"</tr>\n";
qe3Table.innerHTML=
“\n”+
“名称\n”+
“状态\n”+
“查看状态\n”+
“\n”;
我得到的错误是“'null'为null或不是对象”
我非常确定我所有的其他错误都与这一个相同,下面是我的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>
问题可能在于:
var qe3Table = document.getElementById("QE3_TABLE");
如果在加载主体之前运行此脚本,则该脚本将不存在。检查变量中是否有任何内容。我尝试了你们两人的修复,但似乎都没有帮助。最后,我转换了表单的所有调用:
qe3TableNew.innerHTML = ("<tr>\n" +" <th>Name</th>\n" +" <th>Status</th>\n" +" <th>View Status</th>\n" +"</tr>\n");
这似乎解决了问题,所以我相信这与IE无法处理对innerHTML的更改有关
感谢大家的帮助。至少有一个问题(可能产生上述症状)是:
window.onload=init();
提示:()
运算符立即执行函数并计算返回值。这反过来可能会允许XHR处理程序(在某些情况下)在DOM可能还没有准备好的时候启动
愉快的编码。将其转换为jQuery(或选择您最喜欢的库)。我打赌它会“自我修复”<顺便说一句,code>window.onload=init()显然是错误的。只是为了确定。。添加一个
位于textSocket.onreadystatechange=fillTables的末尾
window.onload=init();