i recently learn perl script.
however, i have some problem.
i made cgi script and i want to execute javascript in there.
but that's not execute.
i write my simple example.
=========================================
#!/perl/bin/perl -w
use CGI ':standard';
print header();
print start_html( -title => "test");
print <<"html";
<script language="JavaScript" type="text/JavaScript">
function test_func(){
alert("mouse over")
}
</script>
html
sub_function1(); # this is sub function that display something
sub_function2(); # this is sub function that display something
sub_function3(); # this is sub function that display something
print "<a href=# onMouseOver=test_func()>mouse on</a>";
========================================================
if you know what i fault something.. plz teach me..
or
if i have to other method, plz tell me.
As far as I know you cannot call javascript from your perl script.
I did look at this a few years ago, but got nowhere.
Reasons are:
* The perl script is executed on the web server before the output is sent to the browser.
* Javascript is then executed in the browser after it has received the output from the webserver.
cristiangjj, try this example script and you should be able to to build from it.....
#!/perl/bin/perl -wmy $random_number = int( rand( 10));
print <<HTML;
Content-type: text/html;<html>
<head>
<title>A Random Number</title><script>
var myNumber = $random_number;function display() {
alert( "Your random number is "+ myNumber);
}
</script><style>
.display { padding: 50px; text-align: center; background: lightblue; }
</style></head>
<body>
<div class='display' onMouseOver='display();'>HOVER HERE TO SEE THE NUMBER!</div>
</body></html>
HTML
Note I do not use the CGI module. But that doesn't mean you can't, I just prefer not to for something so simple.
The only things you have wrong in your example really are:
# Shouldn't have "'s here.
print <<"html";
# Should really use ' marks around the parameters on any element.
print "<a href=# onMouseOver=test_func()>mouse on</a>";
I don't know how much HTML the CGI part is filling in for you, if you view the source in the browser you can check if any tags are missing, I'm really thinking about </body></html> unless it does them automatically with sub END.
print qq¦
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<script type="text/javascript" src="/some-directory/some-script.js"></script>
</head>
<body>
<a href="#" onMouseOver="test_func()">mouse on</a>
</body>
</html>
¦;
You can still do this even if you need to use values from the script in your javascript. I still use externals in this case, with markers for the values:
$some_number = 12;open (FILE, "/path/to/javascript.js") ¦¦ die("Cannot open file $!");
while ($line = <FILE>) {
if ($line =~ /\¦SOME\_NUMBER\¦/) { $line =~ s/\¦SOME\_NUMBER\¦/$some_number/; }
$js .= $line;
}
close (FILE);
print qq¦
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<script type="text/javascript">
$js
</script>
</head>
<body>
<a href="#" onMouseOver="test_func()">mouse on</a>
</body>
</html>
¦;
To which the js would look something like
function test_func() {
alert('¦SOME_NUMBER¦');
}
$some_number = 12;
open (FILE, "/path/to/javascript.js") ¦¦ die("Cannot open file $!");
while ($line = <FILE>) {
if ($line =~ /\¦SOME\_NUMBER\¦/) { $line =~ s/\¦SOME\_NUMBER\¦/$some_number/; }
$js .= $line;
}
close (FILE);
you could do something like this:
$some_number = 12;
open (FILE, "/path/to/javascript.js") ¦¦ die("Cannot open file $!");
my $js = do {local $/; <FILE>};
close (FILE);
$js =~ s/(\$some_number)/$1/ees;
where $some_number is literally in the javascript source file:
var = $some_number