Click to See Complete Forum and Search --> : correct way to have JavaScript in a Perl script
crmpicco
10-15-2006, 11:55 AM
Can anyone tell me the correct way to have JavaScript inside a Perl script?
Should it be:
print "Content-type: text/html\n\n";
print "<head>";
print "<script language='javascript' src='javascriptname.js'></script>"
print "</head>";
Is that the correct way or does that not work?
Picco
crmpicco
10-15-2006, 11:57 AM
also, other than keep typing 'print' all the time is there another way to do that? or is it mandatory (perl nooB)
#!/usr/bin/perl
print "Content-Type: text/html\n\n";
print <<END_HTML;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Javascript Test</title>
<script type="text/javascript" src="/javascriptname.js"></script>
</head>
<body>
</body>
</html>
END_HTML
or using CGI.pm
#!/usr/bin/perl
use CGI ':standard';
print header,
start_html(-title => 'Javascript Test',
-script => {-src => '/javascriptname.js'}),
end_html;
crmpicco
10-18-2006, 10:27 AM
Hi CyCo,
Thank you for your help, I have just tried your code (the second example you gave).
This is my file 'form.pl'
#!C:/Perl/bin/perl.exe
use CGI ':standard';
print "Content-type: text/html\n\n";
print "<head>";
print "<title>Perl: Form Processing by Picco</title>";
print header,
start_html(-title => 'JavaScript Test',
-script => {src => '/jstest.js'}),
end_html;
print "</head>";
print "<body bgcolor='#FFFF00'>";
print "<form name='form' method='post' action='processForm.pl'>";
print "<table border='1' bordercolor='#FF00FF'>";
print "<tr>";
print "<td colspan='2'>Please enter your forename</td>";
print "</tr>";
print "<tr>";
print "<td>";
print "<input type='text' name='forename'>";
print "</td>";
print "<td>";
print "<input type='submit' name='submit' value='Submit' onClick='testFunc();'>";
print "</td>";
print "</tr>";
print "</table>";
print "</form>";
and this is my basic JavaScript file, 'jstest.js':
# JavaScript Document
document.write ('JAVASCRIPT!!!');
function testFunc()
{
alert('Good old JavaScript working bang-on!');
}
I'm not getting a 'file not found' error or anything, but when i click on the form submit then i get a 'object expected' error!!!!
Can you see why this would happen?
Cheers.
Picco
I suspect that the location of the js file does not match the path. Where is your js file?
Also, you are using mixed methods in the construction of the HTML. Use one style or the other. Using CGI.pm, the basic structure is as below:
#!/usr/bin/perl
use CGI qw/:standard :no_xhtml/;
print header,
start_html(-title => 'Perl: Form Processing by Picco',
-script => {-src => '/jstest.js'}),
start_form(-action => 'processForm.pl'),
table({-style => 'border:1px solid #ff00ff'},
Tr(td({-colspan => 2}, 'Please enter your forename')),
Tr(td([textfield('forename'),
submit(-name => 'submit',
-value => 'Submit',
-onclick => 'testFunc();')]))),
end_form,
end_html;