mikeyb

msg:3339899 | 9:26 am on May 15, 2007 (gmt 0) |
Hi, 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.
|
Dabrowski

msg:3339987 | 12:40 pm on May 15, 2007 (gmt 0) |
Mikey, by the looks of the script this is what was intended. I use scripts similar to this all the time as I write back-end systems. cristiangjj, try this example script and you should be able to to build from it..... | #!/perl/bin/perl -w my $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.
|
perl_diver

msg:3340202 | 4:40 pm on May 15, 2007 (gmt 0) |
or properly using the CGI module: use CGI ':standard'; my $js = q~function test_func(){ alert("mouse over") }~; print header(), start_html( -title => "test", -script=>{-code=>$js}), '<a href="#" onMouseOver="test_func()">mouse on</a>', end_html;
|
|
|
rocknbil

msg:3340327 | 7:18 pm on May 15, 2007 (gmt 0) |
Two more cents, it's much easier to maintain and execute if you just store your JS externally, like any other page. 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¦'); }
|
perl_diver

msg:3340661 | 4:15 am on May 16, 2007 (gmt 0) |
The CGI module also handles source files: start_html( -title => "test", -script=>{-src=>$jsurl}), where $jsurl would be the absolute or relative URL of the javascript file. Or an array of multiple javascript files: start_html( -title => "test", -script=>{-src=>[$jsurl1,$jsurl2,$jsurl3]}),
|
perl_diver

msg:3340663 | 4:30 am on May 16, 2007 (gmt 0) |
instead of this: $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
|
|