You'd better remember me this Kwanzaa.

Take one; live, nude CGI

use strict; use warnings; use CGI qw( header start_html end_html h1 start_script end_script ); print header(), start_html(-title => "I swear to RTFM in the future", -head => [ start_script({ -type=>'text/javascript', -src=>'/main1.js' }), end_script(), start_script({ -type=>'text/javascript', -src=>'/main2.js' }), end_script(), ] ), h1("Oh, hai! I can haz script?"); print <<"SomeJS"; <script type="text/javascript"><!--//--><![CDATA[//><!-- alert("Oh, noes!"); //--><!]]> </script> SomeJS print end_html();

Take 2; with Template/__DATA__

use strict; use warnings; use Template; use CGI qw( header ); my @scripts = qw( main1.js main2.js ); print header(); my $tt2 = Template->new; $tt2->process(\*DATA, { title => "", script_includes => \@scripts, script_body => "alert('Oh, noes!');", }) or warn $tt2->error; exit 0; __DATA__ <!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-U +S"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> <title>[% title || "I swear to RTFM in the future" %]</title> [%- FOR script IN script_includes %] <script type="text/javascript" src="/[% script %]"></script> [%- END %] </head> <body> <h1>Oh, hai! I can haz script?</h1> <script type="text/javascript"><!--//--><![CDATA[//><!-- [% script_body %] //--><!]]> </script> </body> </html>

To reiterate. The right way to do this is with separation of concerns; template apart from data apart from controller. I don't advocate doing either one of those except for throwaways, personal scripts, or to simplify installation for a CGI that is guaranteed never to grow.


In reply to Re^3: Add Javascript file and Javascript code using CGI.pm by Your Mother
in thread Add Javascript file and Javascript code using CGI.pm by edencraze

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.