How to embed javascript functionality into a Perl CGI script?

V

Vibhu

I have this fundamental problem where I am trying to generate some
HTML content using Perl CGI script. Currently, my CGI script uses some
variables to calculate and value retrieval stuff and throws that onto
a HTML format. In addition to this, I want to add dynamic capability
to my CGI script.

For example, I generate a HTML web-page using Perl CGI code. In that
generated page, if I have some checkboxes/radio buttons/select windows
etc, I want to generate some dynamic content if I select one of those
elements. This means as soon as I select an item, one of the perl
functions should be invoked. Similarly, how could I do "onMouseOver",
"onClick" etc kind of stuff with Perl CGI scripts? Please help me.

Thanks
-Vibhu
 
B

Brendon Caligari

Vibhu said:
I have this fundamental problem where I am trying to generate some
HTML content using Perl CGI script. Currently, my CGI script uses some
variables to calculate and value retrieval stuff and throws that onto
a HTML format. In addition to this, I want to add dynamic capability
to my CGI script.

For example, I generate a HTML web-page using Perl CGI code. In that
generated page, if I have some checkboxes/radio buttons/select windows
etc, I want to generate some dynamic content if I select one of those
elements. This means as soon as I select an item, one of the perl
functions should be invoked. Similarly, how could I do "onMouseOver",
"onClick" etc kind of stuff with Perl CGI scripts? Please help me.

Thanks
-Vibhu

CGI scripts are executed at the server side. And server side code can
only be executed when page in question is requested from the server. To
achieve what you are after you need code that can execute within the
browser itself...such as JavaScript.

B.
 
V

Vibhu

Brendon,

I agree with you. Does it mean we can't do dynamic magic in CGI Perl?
If so, how can it happen? I had prior experience with JSP and
Javascript. JSP executes on server side and Javascript on the client.

What actually happens is when Javascript code is embedded inside a
server side script, client side code won't get interpreted by the
server. Server executes the server side part of it and evaluates it's
values and returns boh HTML and Javascript to the browser(client).

Client then displays HTML pages along with values returned by server.

I did this in JSP. But haven't ever seen how this can be done in perl.
So, I'd appreciate help.

Thanks again.
-Vibhu
 
T

Tad McClellan

[ Please do not top-post! It is seen as being rude. ]




I'm quite sure I don't know what you mean when you say "dynamic".

I think you mean "client side".



Perl code does not run on clients (browsers), it runs on servers.

I agree with you.


It was a statement of fact, not of opinion.

Does it mean we can't do dynamic magic in CGI Perl?
If so, how can it happen? I had prior experience with JSP and
Javascript. JSP executes on server side and Javascript on the client.

What actually happens is when Javascript code is embedded inside a
server side script, client side code won't get interpreted by the
server.


When Javascript code is embedded inside a Perl server side script,
client side code won't get interpreted by the server.

So far so good...

Server executes the server side part of it and evaluates it's
values


A Perl program can do that. Good!

and returns boh HTML and Javascript to the browser(client).


A Perl program can do that too. Good!

Client then displays HTML pages along with values returned by server.


What are these "values returned by server"?

I did this in JSP. But haven't ever seen how this can be done in perl.


Use Perl's print() function to output Javascript in the right place
in the CGI program's output.

That is, JSP embedded Javascript in its output, you can embed
Javascript in output from Perl programs too.
 
C

Chris

Vibhu wrote via top-posting :-( :
Brendon,

I agree with you. Does it mean we can't do dynamic magic in CGI Perl?
If so, how can it happen? I had prior experience with JSP and
Javascript. JSP executes on server side and Javascript on the client.

What actually happens is when Javascript code is embedded inside a
server side script, client side code won't get interpreted by the
server. Server executes the server side part of it and evaluates it's
values and returns boh HTML and Javascript to the browser(client).

Client then displays HTML pages along with values returned by server.

I did this in JSP. But haven't ever seen how this can be done in perl.
So, I'd appreciate help.

Thanks again.
-Vibhu

Same as you would output HTML via Perl. A short contrived example:

#!/usr/bin/perl
$|++;

use strict;
use warnings qw( all );

my $count = 7;

print <<EndHTML;
Content-type: text/html

<html>
<head>
<script language="javascript">
<!--
function button1_onClick() {

alert( 'I output $count lines in $count font sizes!' );

}
//-->
</script>
</head>
<body bgcolor="white" text="black">
<script language="javascript">
<!--
for (var i=1; i<=$count; i++) {
document.write( '<font size="+' + i + '">This is dynamic content
using Perl and JavaScript!</font><br>' );
}
-->
</script>
<form name="frmExample">
<input type="button" name="button1" value="Click Me!"
onMouseOver="parent.status = 'You cant do onMouseOvers in Perl!'; return
true;" onMouseOut="parent.status=''; return true;"
onClick="button1_onClick();">
<!-- Unless you use PerlScript from ActiveState, but that's not a
portable solution! -->
</form>
</body>
</html>
EndHTML

__END__

-ceo
 

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,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top