Welcome to WebmasterWorld Guest from 54.196.232.162

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

how can i execute javascript in perl.

execute javascript in perl.

     
8:15 am on May 15, 2007 (gmt 0)

New User

5+ Year Member

joined:May 15, 2007
posts:1
votes: 0


hello.

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.

9:26 am on May 15, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 21, 2004
posts: 54
votes: 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.

12:40 pm on May 15, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Mar 29, 2007
posts:1147
votes: 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.

4:40 pm on May 15, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 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;
7:18 pm on May 15, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Nov 28, 2004
posts:7999
votes: 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¦');
}
4:15 am on May 16, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 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]}),

4:30 am on May 16, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 5, 2006
posts:536
votes: 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

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members