在 Perl-CGI 生成的页面中包含 JavaScript

发布于 2024-09-30 19:16:18 字数 175 浏览 0 评论 0原文

我试图将 java 脚本添加到动态生成的页面中。 我尝试过这个,但似乎不起作用。

<SCRIPT SRC=\"sorttable.js\"></SCRIPT>

我总是必须将 javacode 与 html 一起内联才能使其工作。 有什么线索吗?

I was trying to add a java-script to a page which is generated on the fly.
I tried this, but it seems like it is not working.

<SCRIPT SRC=\"sorttable.js\"></SCRIPT>

I always have to inline javacode along with the html for it to work.
Any clues ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百合的盛世恋 2024-10-07 19:16:18

qq() 相当于 "",但具有匹配的分隔符。如果您要输出 HTML 或 JavaScript,它将成为您的朋友。

print qq(<script type="text/javascript">alert("The world is my oyster");</script>);

请注意,您不必使用 () 作为分隔符,请参阅 perlop

如果要输出构建 HTML 的 JavaScript,则应该使用 jQuery分机。但无论哪种方式,你都将处于多层的逃离地狱之中。 JSON::XS 可能会让你的生活不再那么痛苦。另请了解此处文档

my $js = <<'JS';
    alert( 'The world is my oyster' );
    var $href = "example.html";
    document.write( '<a href="' + $href + '">clicky</a>' );
JS
print qq(<script type="text/javascript">$js</script>);

上述内容的棘手之处在于 $href 是 JavaScript 变量,而不是 Perl 变量。 (是的,JS 标识符可能包括 $。)

qq() is the equivalent of "", but with matching delimiters. It is going to be your friend if you are outputing HTML or JavaScript.

print qq(<script type="text/javascript">alert("The world is my oyster");</script>);

Note that you don't have to use () as delimiters, see perlop.

If you are outputting JavaScript that is building HTML, you should be using jQuery or Ext. But either way you will be in the multiple-levels-of-escaping-hell. JSON::XS might make your life less painful. Also learn about here-documents:

my $js = <<'JS';
    alert( 'The world is my oyster' );
    var $href = "example.html";
    document.write( '<a href="' + $href + '">clicky</a>' );
JS
print qq(<script type="text/javascript">$js</script>);

The tricky bit about the above is that $href is a JavaScript variable, not a Perl variable. (Yes, JS identifiers may include $.)

将军与妓 2024-10-07 19:16:18

也许这个链接可能会有所帮助:
http://perlmeme.org/tutorials/cgi_form.html

它提供了嵌入jsp函数的方法进入 form-onsubmit 如下:

打印$q->start_form(
-名称=> '主窗体',
-方法=> '得到',
-enctype=> &CGI::URL_ENCODED,

 -onsubmit =>; '返回 javascript:validation_function()',
  -动作=> '/where/your/form/gets/sent', );

并且有一个来自 Perl5 CGI 库的以下链接 - 支持 Javascript,它是关于将 javascript 函数链接到事件。
http://cpansearch.perl.org/src/MARKSTOS /CGI.pm-3.60/cgi_docs.html#javascripting

问候

Perhaps this link might be helpful:
http://perlmeme.org/tutorials/cgi_form.html

It provides method of embedding a jsp function into the form-onsubmit as follow:

print $q->start_form(
-name => 'main_form',
-method => 'GET',
-enctype => &CGI::URL_ENCODED,

  -onsubmit => 'return javascript:validation_function()',
  -action => '/where/your/form/gets/sent',   );

And there is a following link from Perl5 CGI library - support for Javascript, it's about linking javascript function to an event.
http://cpansearch.perl.org/src/MARKSTOS/CGI.pm-3.60/cgi_docs.html#javascripting

Regards

墨洒年华 2024-10-07 19:16:18

好吧,这取决于您对整个事物的引用结构。如果您在未插值的 heredoc 中打印此内容,则 \" 只会产生更大的问题。

   print <<'END_HTML';
   ...
      <SCRIPT SRC=\"sorttable.js\"></SCRIPT>
   ...
   END_HTML

或 aq 表达式:

  print q~
   ...
      <SCRIPT SRC=\"sorttable.js\"></SCRIPT>
   ...
   ~;

因此您必须显示更多上下文。但让我向你保证:当我以正确的方式写出标签时,我的 JavaScript 文件就会被获取到页面中,正如我所期望的那样。

Well it depends on your quoting structure for the WHOLE thing. If you're printing this out in a uninterpolated heredoc, then \" just creates a bigger problem.

   print <<'END_HTML';
   ...
      <SCRIPT SRC=\"sorttable.js\"></SCRIPT>
   ...
   END_HTML

or a q expression:

  print q~
   ...
      <SCRIPT SRC=\"sorttable.js\"></SCRIPT>
   ...
   ~;

So you would have to show more of your context. But let me assure you: when I write out the tags the right way, my JavaScript files gets sourced into the page, just as I would expect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文