简体   繁体   English

为什么字符在 Internet Explorer 中“未定义”,而在其他浏览器中却没有

[英]Why characters are 'undefined' in internet explorer but not in other browsers

Please take a look at the site tdsoft.se The script on that page works in opera, firefox chrome etc and prints out "random_1" as it is suposed to do, but in internet explorer it just prints out (" undefinedundefinedundefinedundefinedundefinedundefinedundefinedundefined "), that is 'undefined' for each letter.请查看站点tdsoft.se 该页面上的脚本适用于歌剧、firefox chrome 等,并按预期打印出“random_1”,但在 Internet Explorer 中它只是打印出(“ undefinedundefinedundefinedundefinedundefinedundefinedundefinedundefinedundefined ”),即对于每个字母都是“未定义的”。 My question is if some of you bright fellows out there might know the answer to this problem?我的问题是,你们中的一些聪明人是否知道这个问题的答案?

EDIT_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ ____编辑_ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ ____

Here's the code这是代码

<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>

[lenY + 1] , for one, does not initialize an array with lenY + 1 elements. [lenY + 1]一方面,不初始化具有lenY + 1元素的数组。 It initializes an array with one element set to lenY + 1 .它初始化一个数组,其中一个元素设置为lenY + 1 Not that that matters, because you set them anyways, to zero... just change that to [] both times.没关系,因为无论如何你都将它们设置为零......只需将其更改为[]两次。

I'm having trouble figuring out your code, but I believe the problem is that IE only allows you to access string characters using charAt , and not using the bracket notation, which you appear to be using here:我在弄清楚您的代码时遇到了麻烦,但我认为问题在于 IE 只允许您使用charAt访问字符串字符,而不是使用括号表示法,您似乎在这里使用它:

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

and here:和这里:

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

So those comparisons would always return true .所以这些比较总是会返回true Could that be the issue?这可能是问题吗?

This is not working in IE, listX [posX-1] .这在 IE, listX [posX-1]中不起作用。 The result of this is "undefined" so you sould use another way to get the char like chatAt() method结果是“未定义”,因此您应该使用另一种方法来获取类似chatAt()方法的字符

Was surprised IE has special meta to specify IE mode.很惊讶 IE 有特殊的元来指定 IE 模式。 Setting it to IE8 solves the issue.将其设置为 IE8 即可解决问题。 Characters are becoming accessible by [] operator. [] 运算符可以访问字符。

See: IE modes请参阅: IE 模式

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 setImmediate和Internet Explorer以外的浏览器 - setImmediate and browsers other than Internet Explorer jQuery无法在Internet Explorer中像在其他浏览器中一样工作 - jQuery not working in Internet Explorer as it does in other browsers 为什么我不能在Internet Explorer中向window.external对象添加方法,但可以在其他浏览器上使用 - why can't I add a method to window.external object in Internet Explorer but working on other browsers 为什么Internet Explorer和Edge与其他浏览器将新的Date()数据输出到数据库不同? - Why does Internet Explorer and Edge output new Date() data into database differently than other browsers? 如何使该texarea javascript在Internet Explorer以外的其他浏览器中工作 - how to make this texarea javascript work in other browsers except Internet Explorer Internet Explorer中的Ajax和JSON响应错误(适用于所有其他浏览器) - Ajax and JSON response bug in Internet Explorer (works in all other browsers) 我在 INTERNET EXPLORER 上的 web 页面上的一些问题,而不是在其他浏览器上 - Some problems on my web page on INTERNET EXPLORER and not on other browsers Internet Explorer中的Bootstrap Accordion与其他浏览器功能不同 - Bootstrap Accordion in Internet Explorer does not function the same as other browsers 当我尝试调试ASP.NET程序时,为什么Internet Explorer(或其他浏览器)使用旧的JavaScript文件? - Why does Internet Explorer (or other browsers) use old JavaScript files when I try to debug my ASP.NET program? 为什么JavaScript样式可用于Internet Explorer以外的所有浏览器? - Why is JavaScript styling working with all browsers except Internet Explorer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM