Skip to content Skip to sidebar Skip to footer

Include Javascript In Perl-cgi Generated Page

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. I

Solution 1:

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

printqq(<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
printqq(<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 $.)

Solution 2:

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

Solution 3:

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~
   ...
      <SCRIPTSRC=\"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.

Post a Comment for "Include Javascript In Perl-cgi Generated Page"