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

I have a header with this Parital code:

print start_html( ... -script => [{-type=>"text/javascript", -src=>"/p +ath/file1.js"}, {-type=>"text/javascript", -src => "path/file2.js"}, +{-type=>"text/javascript", -src => "$_js_path"}, {-code=> "$jscript"} +], ...
That works. Now, if I need a certain page to load another file, I figured I could do this instead:
if($otherBlocksOfCode[0]) {# Already created... # push the file1.js to this array... push(@otherBlocksOfCode, {-type=>"text/javascript", -src=>"/path/file1 +.js"}); } else { # No other page created it, so create it now... my @otherBlocksOfCode; push(@otherBlocksOfCode, {-type=>"text/javascript", -src=>"/path/file1 +.js"}); } print start_html( ... -script => [@otherBlocksOfCode, {-type=>"text/ja +vascript", -src => "path/file2.js"}, {-type=>"text/javascript", -src +=> "$_js_path"}, {-code=> "$jscript"}], ...

But that did not work.

How can I dynamically add more scripts if other pages need more in the header?

Thanks.
Richard

Replies are listed 'Best First'.
Re: header - javascript
by Corion (Patriarch) on Jul 28, 2013 at 11:25 UTC

    How did it not work?

    HTML files (and CGI output) are just text. Please compare the output of one method to the output of the other method. The difference in text output is likely the difference in behaviour.

      I addded a '' around the {}... then reloaded it, so now I see this in the source code output:
      <script type="text/javascript">//<![CDATA[ {-type=>"text/javascript", -src=>"http://ajax.googleapis.com/ajax/libs +/jquery/1.10.2/jquery.min.js"} //]]></script> <script type="text/javascript">//<![CDATA[ {-type=>"text/javascript", -src=>"/path/file1.js"} //]]></script>
      So that did not work either.

      Code I changed:
      from: push(@_extraJSCode, {-type=>"text/javascript", -src=>"http://ajax.goog +leapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"}); to: push(@_extraJSCode, '{-type=>"text/javascript", -src=>"http://ajax.goo +gleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"}');

        Adding single quotes will simply pass things through as strings. I highly doubt that this is what you want to achieve.

        If the Javascript does not get added, you are either not calling the script you think, or the code branch you think, or the array you think you're adding is empty.

        Please show a short (20 lines) self-contained example that exhibits the problem.

      I did not see the javascript code in the HTML...

      I loaded the page, made sure it was a fresh reload, (cleared cache, ect)... checked source code, no javascript in the head.
Re: header - javascript
by Anonymous Monk on Jul 28, 2013 at 11:38 UTC
    I figured it out... instead of doing a push... the way I was doing it:
    if($otherBlocksOfCode[0]) {# Already created... # push the file1.js to this array... push(@otherBlocksOfCode, {-type=>"text/javascript", -src=>"/path/file1 +.js"}); } else { # No other page created it, so create it now... my @otherBlocksOfCode; push(@otherBlocksOfCode, {-type=>"text/javascript", -src=>"/path/file1 +.js"}); } print start_html( ... -script => [@otherBlocksOfCode, {-type=>"text/ja +vascript", -src => "path/file2.js"}, {-type=>"text/javascript", -src +=> "$_js_path"}, {-code=> "$jscript"}], ...
    I just added it this way:
    @_extraJSCode = ({-type=>"text/javascript", -src=>"http://ajax +.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"},); # then at the main header, where it is built, I do this: if($_extraJSCode[0]) { push(@_extraJSCode, {-type=>"text/javascript", -src=>"/path/fi +le1.js"}); } else { @_extraJSCode = ({-type=>"text/javascript", -src=>"/jsfiles/st +_utilities.js"}); }
    and that did work.

    so it is resolved.

    Richard

      } else { # No other page created it, so create it now... my @otherBlocksOfCode; # <===== lexically scoped to else block push(@otherBlocksOfCode, {-type=>"text/javascript", -src=>"/path/file1 +.js"}); }
      Your else block is the containing block for the my @otherBlocksOfCode;. When the else is completed, that variable no longer exists. In other words, if you remove the my @otherBlocksOfCode; line, it should work how you expect.

      --MidLifeXis