2012-01-06 128 views
0

這應該很簡單 - 不要弄錯我做錯了!這是一個非常基本的測試(我是新來的PERL和JavaScript) - 這是CGI文件:從Perl調用Javascript函數

#! /usr/local/bin/perl 

print "Content-type: text/html\n\n"; 
print "<html>\n" ; 
print "<head>Hello\n"; 
print '<script type="text/javascript" language="javascript" src="wibble.js">\n'; 
print "</script>\n"; 
print "</head>\n"; 

print "<body>\n"; 

$fred = "Fred"; 
$numb = 7; 

print <<TEST; 

<p>Starting...</p> 
<p><script type="text/javascript" language="javascript"> 
theText = "$fred"; 
theNum = "$numb"; 
document.writeln("Direct write..."); 
document.writeln("Number is: " + theNum); 
document.writeln("Text is: " + theText); 

testWrite(theNum, theText); 

</script></p> 

<p>...ending JS</p> 

TEST 

和wibble.js:

function testWrite(num1, txt1) 
{ 
    document.writeln("In testWrite..."); 
    document.writeln("Number is: " + num1); 
    document.writeln("Text is: " + txt1); 
} 

在我的瀏覽器,我得到的第一套writeln的,但我的功能從來沒有被調用過。網頁上的錯誤顯示第15行('print <<TEST'行)處的'Object expected'。

我主要懷疑我的src元素沒有找到正確的路徑,但我嘗試了所有我能想到的組合('。','./',完整路徑等) - 沒有任何作用。 js文件與CGI文件位於相同的目錄中。 (我實際上最初有沒有參數的函數調用,希望theNum和theText是全局的並且仍然能夠工作(這是這個測試程序的原始點))。

請把我從我的痛苦......

按照要求,這裏是從瀏覽器源代碼:

<html> 
<head><script type="text/javascript" language="javascript" src="wibble.js"></script> 
</head> 
<body> 

<p>Starting...</p> 
<p><script type="text/javascript" language="javascript"> 
theText = "Fred"; 
theNum = "7"; 
document.writeln("Direct write..."); 
document.writeln("Number is: " + theNum); 
document.writeln("Text is: " + theText); 

testWrite(theNum, theText); 

</script></p> 

<p>...ending JS</p> 

</body> 
</html> 

,這是網頁上的實際輸出:

Starting... 

Direct write... Number is: 7 Text is: Fred 

...ending JS 
+0

單引號的字符串不能將'\ n'理解爲換行符。檢查腳本的輸出。我除了在'