Why can't JavaScript work when embeded in Perl CGI code?

D

Dave

Greetings

I had a working Perl CGI code which generates HTML page based on the
paramaeters.

I want to add a little JavaScript function like
=========================================
<SCRIPT language="JavaScript">
<!--

//Function go_download to pop up confirm window
function go_download()
{
var is_sure = window.confirm("Are you sure you want to download?");

if (is_sure == true)
{
print "<input type=submit value=\"Get Patches\">";
print end_html();
}
}

<FORM>
<INPUT type ="button" value = "Click to Get Patches"
onClick = "go_download();">
</FORM>

//-->
</SCRIPT>
=========================================================

But it comes error like this
"500 Server Error
The server encountered an internal error or misconfiguration and was unable
to complete your request."
 
R

Rick Scott

(Dave said:
I had a working Perl CGI code which generates HTML page based on the
paramaeters.

.... (changes made) ...
But it comes error like this
"500 Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request."

This generally means that there is an error with your Perl CGI script
that prevents it from being executed; it usually has nothing to do
with the output the script generates. If your script was working
before you added the javascript output clause, and isn't now, I'd
humbly suggest that you broke something in the process.

Have a look in the webserver error logs -- you should be able to get
some idea of what's causing your script not to run. Alternatively,
run your script from the command line and see what happens. If you're
using the CGI module, it has debugging tools that'll help you out.

perldoc CGI




Rick
 
J

John Bokma

Dave said:
Greetings

I had a working Perl CGI code which generates HTML page based on the
paramaeters.

I want to add a little JavaScript function like

Don't "like", always give the smallest program that exhibits the problem

Try perl -c script.cgi
 
A

A. Sinan Unur

Greetings

I had a working Perl CGI code which generates HTML page based on the
paramaeters.

I want to add a little JavaScript function like
=========================================
<SCRIPT language="JavaScript">
<!--

//Function go_download to pop up confirm window
function go_download()
{
var is_sure = window.confirm("Are you sure you want to download?");

if (is_sure == true)
{
print "<input type=submit value=\"Get Patches\">";
print end_html();
}
}

Is this supposed to be Javascript or Perl?

Try perl -c myscript.pl, and take a look at all the syntax and parsing
errors. You cannot intermix Javascript and Perl in a Perl program.

You need to print the Javascript code along with the HTML.

The easiest way to do this is to use a template for the HTML output
(such as HTML::Template) where you can debug and correct the Javascript
code independently of Perl.

Sinan

PS: Please read the posting guidelines.
 
F

frank.barragan

I've come up on this type of issue before, and it's always related to
an error in the Perl file. I usually run it from the command line, and
the errors it generates from there clearly indicated what is wrong.
 
A

Andrew

A. Sinan Unur said:
Is this supposed to be Javascript or Perl?

Try perl -c myscript.pl, and take a look at all the syntax and parsing
errors. You cannot intermix Javascript and Perl in a Perl program.

While "perl -c myscript.pl" should be enough in most cases, it is
perhaps worth mentioning that looking in the CGI error logs may at
times be very useful.

Theoretically speaking, "perl -c myscript.pl" may not show an error
while myscript.pl is failing as CGI, with a 500 error: "require"d
scripts are not checked, or "require"s may only be triggered over CGI,
or an absent %ENV value (or some other CGI-dependent value) causes an
eval(<blah>) to fail to define a subroutine... etc.etc. (I realize the
latter is bad coding, but we're talking about development, which is
precisely the time one runs into tricky problems, and looking in the
error logs is sometimes the most efficient way to identify these.)

Plus, perl syntax error descriptions are very helpful and usually lead
you straight to the problem.


andrew
 
G

Gunnar Hjalmarsson

Dave said:
"500 Server Error...

In addition to the debugging methods already mentioned, a convenient way
to get more useful error messages written to screen is to add the line

use CGI::Carp 'fatalsToBrowser';

right below the shebang line.
 
T

Tad McClellan

Dave said:
Subject: Why can't JavaScript work when embeded in Perl CGI code?


Because JavaScript and Perl are different languages.

I had a working Perl CGI code which generates HTML page based on the
paramaeters.

I want to add a little JavaScript function


Then put it in a Perl print() statement.

CGI programs run on the server.

JavaScript runs on the client (browser), so you need to send
the code to the client for execution.

But it comes error like this
"500 Server Error
The server encountered an internal error or misconfiguration and was unable
to complete your request."


perldoc -q 500

My CGI script runs from the command line but not the browser. (500
Server Error)


What did it say in your server error log?
 
T

Todd

Dave said:
Greetings

I had a working Perl CGI code which generates HTML page based on the
paramaeters.

I want to add a little JavaScript function like

Dave,

Lincoln Stein has documented CGI.pm very well. You should read it:
http://search.cpan.org/dist/CGI.pm/CGI.pm.

Look at the following for a template:

Todd

#!/my/path/to/perl/bin/perl
################################################################################
## modules

################################################################################
use strict;
use warnings;
use diagnostics;
use CGI;
################################################################################
## globals

################################################################################
my $html = new CGI;
my $style1 = '/css/content.css';
my $style2 = '/css/layout.css';
my $common_js = '/javascript/common.js';
################################################################################
## control

################################################################################
&start;
&content;
&tail;
################################################################################
## body
################################################################################
sub content
{
print $html->button(-name=>'button_name',
-id=>'button_name',
-value=>'user visible label',
-onClick=>"go_download()");
}
################################################################################

## header
################################################################################
sub start
{
print $html->header();
print $html->start_html(-title=>'CGI & JS',
-style=>{-src=>[$style1,$style2],
-code=> &local_css },
-script=>[
{ -language=> 'JavaScript',
-src=> $common_js
},
{ -language=> 'JavaScript',
-code=> &local_js
},
]
);
}
################################################################################

## End
################################################################################
sub tail
{
print $html->end_form();
print $html->end_html();
}
################################################################################

## CSS inside of Perl
################################################################################
sub local_css
{
my $this_css = '';

$this_css = qq(a{text-decoration:none;});

return $this_css;
}
################################################################################

## JS inside of Perl
################################################################################
sub local_js
{
my $this_js = '';
$this_js = <<JS_END;
function go_download()
{
if (confirm("Are you sure you want to download?"))
{
alert("it works!")
}
}
JS_END
return $this_js;
}
__END__
 
T

thrill5

The problem is that you </SCRIPT> tag is at the end of the output, not at
the end of the actual JavaScript function

See below

Scott
Dave said:
Greetings

I had a working Perl CGI code which generates HTML page based on the
paramaeters.

I want to add a little JavaScript function like
=========================================
<SCRIPT language="JavaScript">
<!--

//Function go_download to pop up confirm window
function go_download()
{
var is_sure = window.confirm("Are you sure you want to download?");

if (is_sure == true)
{
print "<input type=submit value=\"Get Patches\">";
print end_html();
}
}
********* Your //--> and said:
<FORM>
<INPUT type ="button" value = "Click to Get Patches"
onClick = "go_download();">
</FORM>
******** Not HERE *************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top