简体   繁体   中英

Reference Error Variable not declared

I've been scratching my head for hours trying to figure this out. But hours later,still no luck. So I've created a function but when I run the code it says im getting a Reference error and saying that the variable isnt declared

Here is my code:

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

Here is my .js file

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");

}

I'd guess it's the boolean . Try removing it.

In Javascript, you don't need to specify a variable's type. So, it probably reads boolean as a variable name, but sees that there is no variable named boolean , so throws a reference error.

Additionally, fileLoading.js contains multiple syntax errors. I'd recommend looking at the errors it should be producing for you…

In particular:

  • Line 26 is missing a right paren
  • Bad quotes on line 44
  • Unexpected int on line 46 (don't need those!)
  • Line 46 needs a 3rd clause in the for-loop
  • Used => instead of presumably >= on line 49
  • Unexpected int on line 53
  • Used =< instead of presumably <= on line 53
  • &nbsp; is an HTML entity, not a Javascript variable; move into the string on line 55
  • And even more; I'm going to bed now, but you should be able to find the rest with your console's help.

This is one of the reasons that I recommend building software in small pieces and testing between each piece: debugging a huge chunk of code like this is seriously painful, and many of these errors wouldn't even exist if you'd caught their first instances early.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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