在加载html页面上运行perl脚本

时间:2015-12-14 08:00:41

标签: javascript html perl

如何在浏览器上加载html页面时运行perl脚本。我试图运行perlscript onload(),甚至将它写在头上。但没有奏效  perl脚本:

#!/usr/bin/perl -w
use strict;
use CGI ':standard';
use warnings;
use FileHandle;

my $result="sakshi";

open(OUT,'>/var/cgi-bin/sample.html');
print 
 OUT header(),
start_html(
    -title   => 'Command',
  -text    => '#520063'
);
print OUT "Hello $result";
print OUT end_html();
close(OUT);

我在html中试过的东西:

1

<head>
<title>test</title>
<script src="common_info.pl"></script>
</head>

2

<body onload=call_perl()">
<script text/javascript>
function call_perl(){
var ajax = new XMLHttpRequest;
ajax.open("GET", "http://localhost/cgi-bin/myscript.pl", true);
ajax.send();
}
</script>

1 个答案:

答案 0 :(得分:0)

在HTML文件中:

<html>
  <head>
    <title></title>
    <script type="text/javascript" src="http://localhost/cgi-bin/my_script.pl"></script>
  </head>
  <body></body>
</html>

在my_script.pl中:

#!/usr/bin/perl

use strict;
use CGI ':standard';
use warnings;
use FileHandle;

my $result="sakshi";

open(OUT, ">", "/var/www/cgi-bin/sample.html") or die "cannot open > /var/www/cgi-bin/sample.html: $!";
print OUT header();
print OUT start_html(-title=>'Command',-text=>'#520063');
print OUT print("Hello $result");
print OUT end_html();
close(OUT);

当我激活HTML文件时,/ var / www / cgi-bin / sample.html文件现在将包含生成的内容:

<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>Command</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body text="#520063">
1
</body>
</html>

希望这可以清除abit