为什么字符在Internet Explorer中是“未定义的”,而在其他浏览器中则不是

时间:2011-07-11 15:30:08

标签: javascript html xmlhttprequest

请查看网站tdsoft.se 该页面上的脚本可以在opera,firefox chrome等中运行,并打印出“random_1”,但是在Internet Explorer中它只是打印出来的(“ undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined n。”) '为每封信。我的问题是,如果你们中的一些聪明人可能知道这个问题的答案吗?

EDIT_的 _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ 的__ _ __ _ __ _ ____

这是代码

<html>
<head>
<script language="javascript" type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
var txt;
var buildName = "";
var xmlhttp;
/**
 * Find a longest common subsenquence.
 *
 * Note: this is not necessarily the only possible longest common subsequence though!
 */
function lcs(listX, listY) {
        return lcsBackTrack(
                lcsLengths(listX, listY),
                listX, listY, 
                listX.length, listY.length);
}

/**
 * Iteratively memoize a matrix of longest common subsequence lengths.
 */
function lcsLengths(listX, listY) {
        var lenX = listX.length;
        var lenY = listY.length;

        // Initialize a lenX+1 x lenY+1 matrix
        var memo = [lenX+1];
        for (var i = 0; i < lenX+1; i++) {
                memo[i] = [lenY+1];
                for (var j = 0; j < lenY+1; j++) {
                        memo[i][j] = 0;
                }
        }

        // Memoize the lcs length at each position in the matrix
        for (var i = 1; i < lenX+1; i++) {
                for (var j = 1; j < lenY+1; j++) {
                        if (listX[i-1] == listY[j-1]) {
                                memo[i][j] = memo[i-1][j-1] + 1;
                        }
                        else {
                                memo[i][j] = Math.max(
                                        memo[i][j-1],
                                        memo[i-1][j]);
                        }
                }
        }

        return memo;
}

/**
 * Recursively read back a memoized matrix of longest common subsequence lengths
 * to find a longest common subsequence.
 */
function lcsBackTrack(memo, listX, listY, posX, posY) {

        // base case
        if (posX == 0 || posY == 0) {
                return "";
        }

        // matcth => go up and left
        else if (listX [posX-1] == listY[posY-1]) {
                return lcsBackTrack(memo, listX, listY, posX-1, posY-1) + listX[posX-1];
        }

        else {
                // go up
                if (memo[posX][posY-1] > memo[posX-1][posY]) { 
                        return lcsBackTrack(memo, listX, listY, posX, posY-1);
                }

                // go left
                else {
                        return lcsBackTrack(memo, listX, listY, posX-1, posY);
                }
        }
}

function loadXMLDoc(url,cfunc)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}


function myFunction()
{

    loadXMLDoc("http://tdsoft.se/testni.html",handleXML);


}
var checkState = function(xmlhttp, callback) {

try{
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        callback();
        } 
        else {
            // Check back again 1 sec later
            setTimeout(checkState, 1000);
        }
    }
    catch(err){
        setTimeout(checkState, 1000);
    }
};


function handleXML()
  {
checkState(xmlhttp, function() {

   txt=xmlhttp.responseText;
buildName = "random_1";
var myvar = "";
txt = "" + txt;
var lcsString = lcs(txt, buildName);
document.write(lcsString);
});
  }
</script>
</head>
<body onLoad="myFunction()">
</body>
</html>

3 个答案:

答案 0 :(得分:1)

例如,

[lenY + 1]不会使用lenY + 1元素初始化数组。它初始化一个数组,其中一个元素设置为lenY + 1。这并不重要,因为你无论如何都将它们设置为零......只需将它改为[]两次。

我无法弄清楚你的代码,但我认为问题是IE只允许你使用charAt来访问字符串字符,而不是使用括号表示法,你似乎在这里使用它:

listX[i-1] == listY[j-1]

在这里:

else if (listX [posX-1] == listY[posY-1]) {

因此,这些比较将始终返回true。这可能是问题吗?

答案 1 :(得分:1)

这不适用于IE,listX [posX-1]。结果是“未定义”,因此您可以使用其他方式获取类似chatAt()方法的字符

答案 2 :(得分:-1)

很惊讶IE有特殊的meta来指定IE模式。 将其设置为IE8解决了这个问题! []运算符可以访问字符。

请参阅: IE modes