PerlRob has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks,

I'm having a hell of a time getting CGI.pm to include an external JavaScript source file. Here's my code...
use CGI qw/:standard/; print header, start_html ( -dtd => '-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml +1/DTD/xhtml1-strict.dtd', -title => "Test", -script => {-type => 'text/javascript', -src=>'../test.js'} ), "Test", end_html;
...and here's the HTML it produces (notice that there's no javascript tag):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w +3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-U +S"> <head> <title>Test</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body> Test </body> </html>
What am I doing wrong?

Thanks, Rob

Replies are listed 'Best First'.
Re: CGI.pm and JavaScript source file
by PerlRob (Sexton) on Jun 04, 2008 at 02:51 UTC
    Finally figured it out. Had to place it in the -head option:
    use CGI qw/:standard/; print header, start_html ( -dtd => '-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml +1/DTD/xhtml1-strict.dtd', -title => "Test", -head => script({-type => 'text/javascript', -src=>'../test.js'}) ), "Test", end_html;

Re: CGI.pm and JavaScript source file
by pc88mxer (Vicar) on Jun 04, 2008 at 02:50 UTC
    Try -language => 'text/javascript' instead of -type => ....

      Try -language => 'text/javascript'

      I wouldn't, it's deprecated and not-supported in some browsers (read: Bad Practice).

      Stop saying 'script'. Stop saying 'line-noise'.
      We have nothing to lose but our metaphors.