javascript - 将 Firefox javascript 转换为 IE javascript

标签 javascript ajax internet-explorer firefox

我一直在尝试让一些在 FireFox 中运行良好的 AJAX 代码在 IE 中运行。 不过,我在更新脚本中的一些表时遇到了一些麻烦。我见过许多其他人也有类似的问题,但他们找到的解决方案对我来说都不起作用。问题首先出现在上线

qe3Table.innerHTML = 
    "<tr>\n" +
    "   <th>Name</th>\n" +
    "   <th>Status</th>\n" +
    "   <th>View Status</th>\n" +
    "</tr>\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");

如果您在加载正文之前运行此脚本,则该脚本将不存在。检查该变量中是否有任何内容。

关于javascript - 将 Firefox javascript 转换为 IE javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5971823/

相关文章:

javascript - 为什么es6需要构造函数?

javascript - ajax函数自动重新加载

javascript - 使用 jQuery 处理表单数据

javascript - 单击事件后无法从 url 中删除主题标签

Javascript交换两个对象a、b,哪种方式更好?

javascript - 动态表格如果复选框为空() td单元格与JS

jquery - 我应该在 jQueryMobile 中使用 jQuery Ajax api 吗?

javascript - 如何让 html5 颜色选择器在 IE 中工作?

javascript - 原型(prototype)观察者在 IE8 中不触发

jquery - IE7 JQuery、Ajax 和 CSS 的问题