繁体   English   中英

未声明参考错误变量

[英]Reference Error Variable not declared

我一直在摸不着头脑试图解决这个问题。 但几个小时后,仍然没有运气。 所以我创建了一个函数,但是当我运行代码时,它说我得到了一个Reference错误,并说该变量未被声明

这是我的代码:

<html>

<head>
<script src = "fileLoading.js"></script>
<script>

function initialLoad(boolean vraiFaux){
fileLoading("inventory.xml", vraiFaux); //boolean to variable
}

//function pageWrite(){
// code here
//}

</script>

</head>

<button type = "button" onClick = "initialLoad(false)">
<img  src = "panda4.png" alt="Does this work?"></img>
</button>

<body id = "body" onload="initialLoad(true)">

</body>

</html>

这是我的.js文件

var xmlDoc;

function fileLoading(dname, vraiFaux)
{

if(vraiFaux){
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",dname,false);
xhttp.send();
xmlDoc = xhttp.responseXML;
}


var products = xmlDoc.getElementsByTagName("PRODUCT");
var saleItems = new Array();
var p = 0; 

for(var i = 0; i<products.length; i++){
if((products[i].getAttribute("sale")).equals("yes"){
document.getElementById("body").innerHTML+=
('<img src = ' + products[i].getElementsByTagName("PIC")[0].childNodes[0].nodeValue + '</img>' ); // need spacing

saleItems[p] = i;
 p++;
if (p == 3)
break;
}

}
} 




function pageWrite(){
document.getElementById("body").innerHTML=
('<table border = '1'>');
var checker = 0;
for(int externalForLoop = 0; externalForLoop < products.length){


if(checker => products.length)
break;
document.getElementById("body").innerHTML += 
('<tr>');
for (int i = 0; i =< 2; i++){
document.getElementById("body").innerHTML += 
('<td><b><img src = ' + products[checker].getElementsByTagName("PIC")[0].childNodes[0].nodeValue + '</img></b></br></td>' + &nbsp);

} 
document.getElementById("body").innerHTML += 
('</tr></br>');
//next for loop goes after here
for (int n = 0; n =< 2; n++){
document.getElementById("body").innerHTML += 
products[checker].getElementsByTagName("NAME")[0].childNodes[0].nodeValue + '</b></br></td>' + &nbsp);
} 
document.getElementById("body").innerHTML += 
('</tr></br>);
//next for loop goes after here
for (int c = 0; c =< 2; c++){
document.getElementById("body").innerHTML += 
products[checker].getElementsByTagName("CATAGORY")[0].childNodes[0].nodeValue + '</b></br></td>' + &nbsp);
} 
document.getElementById("body").innerHTML += 
('</tr></br>);
//next for loop goes after here
for (int o = 0; o =< 2; o++){
document.getElementById("body").innerHTML += 
products[checker].getElementsByTagName("COMPANY")[0].childNodes[0].nodeValue + '</b></br></td>' + &nbsp);
} 
document.getElementById("body").innerHTML += 
('</tr></br>);
//next for loop goes after here
for (int d = 0; d =< 2; d++){
document.getElementById("body").innerHTML += 
products[checker].getElementsByTagName("DESCRIPTION")[0].childNodes[0].nodeValue + '</b></br></td>' + &nbsp);
} 
document.getElementById("body").innerHTML += 
('</tr></br>);
//next for loop goes after here
for (int p = 0; p =< 2; p++){
document.getElementById("body").innerHTML += 
products[checker].getElementsByTagName("PRICE")[0].childNodes[0].nodeValue + '</img></b></br></td>' + &nbsp);
} 
document.getElementById("body").innerHTML += 
('</tr></br>);
//next for loop goes after here
for (int s = 0; s =< 2; s++){
document.getElementById("body").innerHTML += 
products[checker].getElementsByTagName("SALE")[0].childNodes[0].nodeValue + '</b></br></td>' + &nbsp);
} 
document.getElementById("body").innerHTML += 
('</tr></br>);
//next for loop goes after here
checker++;
if (checker == (products.length - 1)){
document.getElementById("body").innerHTML += 
('</table>')
}

var products=xmlDoc.getElementsByTagName("PRODUCT");

}

我猜它是boolean 尝试删除它。

在Javascript中,您不需要指定变量的类型。 因此,它可能将boolean作为变量名称读取,但是看到没有名为boolean变量,因此抛出了引用错误。

此外, fileLoading.js包含多个语法错误。 我建议看看它应该为你制作的错误......

尤其是:

  • 第26行缺少右边的paren
  • 第44行报价不佳
  • 第46行意外的int (不需要那些!)
  • 第46行需要for循环中的第3个子句
  • 在第49行使用=>而不是大概>=
  • 第53行意外的int
  • 使用=<而不是大概<=第53行
  • &nbsp; 是一个HTML实体,而不是一个Javascript变量; 进入第55行的字符串
  • 甚至更多; 我现在要睡觉了,但你应该能够在你的控制台的帮助下找到其余的。

这是我推荐在小块中构建软件并在每个部分之间进行测试的原因之一:调试像这样的大量代码是非常痛苦的,如果你发现它们的第一个实例,许多这些错误甚至都不存在早。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM