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

Hi guys, I've been breaking my head for a long time trying to figure out if there is a way to include a javascript source file and javascript code stored in a variable using methods of CGI.pm. I have been able to do this by directly typing HTML code into the page. BUT that's not kinda impressive to look at.
  • Comment on Add Javascript file and Javascript code using CGI.pm

Replies are listed 'Best First'.
Re: Add Javascript file and Javascript code using CGI.pm
by Your Mother (Archbishop) on Jul 02, 2008 at 22:16 UTC

    Stop breaking your head. Give the documentation a chance! From CGI's Pod (modified for correct attributes)-

    print $q->start_html(-title=>'The Riddle of the Sphinx', -script=>{-type=>'text/javascript', -src=>'/javascript/sphinx.js'} );

    You can also just use HEREdocs to print scripts out without getting too messy. Templates are the way to go but I do this sort of thing for one-offs and standalones. Sometimes TT2 in a standalone with all the template code in the __DATA__ section.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Add Javascript file and Javascript code using CGI.pm
by olus (Curate) on Jul 02, 2008 at 22:10 UTC

    I don't fully see your question, but you'll have to add the javascript code, or the link to the source, on the <script> tags to the HTML page. If what you don't like to look at is the HTML inside the Perl script I'd suggest using a template approach. I like Template::Toolkit.