在Perl-CGI生成的页面中包含JavaScript [英] Include JavaScript in Perl-CGI generated page

查看:130
本文介绍了在Perl-CGI生成的页面中包含JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个java脚本添加到即时生成的页面中。
我试过了,但看起来不起作用。

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>

我总是必须将javacode和html一起内联才能工作。
任何线索?

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

推荐答案

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

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>);

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

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

如果您正在输出构建HTML的JavaScript,则应该使用 jQuery 或< a href =http://www.sencha.com/products/js/ =nofollow> Ext 。但无论哪种方式,你都会处于多层次的逃跑 - 地狱。 JSON :: XS 可能会让你的生活更轻松。同时了解 here-documents

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>);

上述棘手的问题是 $ href 是一个JavaScript变量,而不是Perl变量。 (是的,JS标识符可能包括 $ 。)

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

这篇关于在Perl-CGI生成的页面中包含JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆