为什么字符是“未定义”的?在 Internet Explorer 中有效,但在其他浏览器中无效
请查看该网站tdsoft.se 该页面上的脚本适用于 Opera、Firefox chrome 等,并按预期打印出“random_1”,但在 Internet Explorer 中它只打印出(“undefinedundefineundefineundefineundefineundefineundefineundefined”),即“未定义” ' 对于每个字母。我的问题是,你们中的一些聪明人是否知道这个问题的答案?
编辑________________< em>_____________ _________________ ______________
这是代码
<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>
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. 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这在 IE 中不起作用,
listX [posX-1]
。其结果是“未定义”,因此您应该使用另一种方式来获取字符,例如chatAt()
方法This is not working in IE,
listX [posX-1]
. The result of this is "undefined" so you sould use another way to get the char likechatAt()
method例如,
[lenY + 1]
不会用lenY + 1
元素初始化数组。它初始化一个数组,其中一个元素设置为lenY + 1
。这并不重要,因为无论如何你都将它们设置为零......只需将其两次更改为[]
即可。我无法弄清楚您的代码,但我相信问题是 IE 只允许您使用
charAt
访问字符串字符,而不是使用您似乎在此处使用的括号表示法:这里:
所以这些比较总是返回
true
。这可能是问题所在吗?[lenY + 1]
, for one, does not initialize an array withlenY + 1
elements. It initializes an array with one element set tolenY + 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:and here:
So those comparisons would always return
true
. Could that be the issue?令人惊讶的是 IE 有特殊的元来指定 IE 模式。
将其设置为IE8即可解决问题!字符可以通过 [] 运算符访问。
看:
<一href="http://www.google.com/url?sa=t&rct=j&q=access%20to%20character%20in%20ie8%20javascript%20undefined&source=web&cd=2&ved=0CDAQFjAB& ;网址=http ://www.nczonline.net/blog/2010/02/02/how-internet-explorer-8-document-mode-affects-javascript/&ei=TU4gT96IJ4yf-wbQt4XEBA&usg=AFQjCNHO8q-fDqLwUjHhvG4msGwgbCcz0A" rel="nofollow noreferrer">IE 模式
Was surprised IE has special meta to specify IE mode.
Setting it to IE8 solves the issue ! Characters are becoming accessible by [] operator.
See:
IE modes