2013-06-02 102 views
0

我一直在摸索着解決這個問題。但幾個小時後,仍然沒有運氣。所以,我創建了一個功能,但是當我運行的代碼,它說即時得到一個參考錯誤,並說該變量的心不是宣佈參考錯誤變量未聲明

這裏是我的代碼:

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

} 
+0

'fileLoading.js'中有什麼? – Hristo

+0

另外,爲什麼您的'

+0

我的猜測是,它無法找到你的'fileLoading.js'文件...你檢查過,看看它是否被成功提取? – Hristo

回答

2

我猜這是boolean。嘗試刪除它。

在Javascript中,您不需要指定變量的類型。因此,它可能讀取boolean作爲變量名稱,但看到沒有名爲boolean的變量,因此會引發引用錯誤。

此外,fileLoading.js包含多個語法錯誤。我建議你看它應該爲您產生的錯誤...

特別是:在線

  • 第26行缺少括號
  • 壞行情右線44
  • 意外int 46(不需要這些!)
  • 線46需要在第3節的for循環
  • 上線49用於=>而不是大概>=
  • 意外int上線53
  • 用於=<代替推測<=上線53
  • &nbsp;是一個HTML實體,而不是一個JavaScript變量;移入55行的字符串
  • 甚至更​​多;我現在要去睡覺,但你應該可以在你的控制檯的幫助下找到其他的。

這是我推薦在小塊中構建軟件並在每個塊之間進行測試的原因之一:調試這樣的一大塊代碼是非常痛苦的,如果你覺得這些錯誤很多都不會存在'早早就抓住了他們的第一個例子。

+0

試過了,但仍然給我同樣的錯誤。錯誤是現在fileLoding沒有被定義。在它初始化之前沒有定義 – user2444526

+0

不,它會拋出'SyntaxError:意外的標識符' – Hristo

+0

這不是你不需要的。在JS中這樣做是錯誤的。變量沒有類型。對象做。 –