如何使用Perl CGI弹出菜单打开新页面



当我们在菜单中选择一个项目时,我希望从下面的Perl-cgi脚本中打开一个新页面;就像我们处理window.open一样。如果有人知道我如何执行类似于window.open('http://www.google.com','_blank','工具栏=0,位置=0,菜单栏=0'(;

提前感谢

#!/usr/bin/perl -Tw
use strict;
use warnings;
use utf8;
use CGI ;
my $cgi = new CGI;
my %url = (
"https://www.google.com"    => "google" ,
"https://www.msn.com"       => "msn",
"https://www.yahoo.com"     => "yahoo" 
);
print $cgi->header("text/html;charset=UTF-8");
print "<!DOCTYPE html>";
print "<html>n";
print "<head>n";
print "</head>n";
print "<body>n";
print '<form>'."n";
print $cgi->popup_menu(
- name     => 'url',
- id       => 'url',
- values   => [sort keys %url],
- default  => ['google'],
- labels   => %url,
- onchange => 'submit();'
#- onchange => "this.form.submit();"
);
print "</form>n";
print "</body>n";
print "</html>n";

您几乎拥有它,但您可能想要this.value

print $cgi->popup_menu(
- name     => 'url',
- id       => 'url',
- values   => [sort keys %url],
- default  => ['google'],
- labels   => %url,
- onchange => q{window.open(this.value, '_blank', 'toolbar=0,location=0,menubar=0');},
);

但正如我所评论的,由于这些CGI方法已被弃用,请考虑使用其他方法,如Template::Toolkit。TT中的一个常见模式是有两个文件,它们可能如下所示。

Perl文件:

#!/usr/bin/perl -Tw
# my_form.cgi
use strict;
use warnings;
use utf8;
use HTTP::Headers;
use Template;
my $headers = HTTP::Headers->new;
$headers->header('Content-Type' =>  'text/html;charset=UTF-8');
print $headers->as_string;
my %urls = (
"https://www.google.com"    => "google" ,
"https://www.msn.com"       => "msn",
"https://www.yahoo.com"     => "yahoo" 
);
my $template = Template->new;
$template->process('my_form.ttml', { urls => %urls });

模板文件:

[%# my_form.ttml %]
<!DOCTYPE html><html>
<head>
</head>
<body>
<form>
<select name="url"  id="url" onchange="window.open(this.value, '_blank', 'toolbar=0,location=0,menubar=0');">
[% FOREACH url IN urls -%]
<option value="[% url.key %]">[% url.value %]</option>
[% END -%]
</select>
</form>
</body>
</html>

有关Template::Toolkit的详细信息,您可以查看Template::Manual和Template::Manual::Intro,也可以查看CGI::Alternatives以获得更多灵感。

最新更新