Please take the advice of all the other posters to this thread. They gave good advice.

It seems that you want to set a variable for your char_by_chars.js script file.

You can't shoehorn a variable's content into a file that is referenced by the browser via a src attribute, other that opening this file, writing the variable's content to it, and close the file. Which surely is not what you want.

But you can include verbatim Javascript with the content of a variable in your page, before the directives containing src tags:

my $data = 'Some text in a variable defined before loading script file +s'; print start_html( -script => [ "var textToShow = '$data';\n", { -language => 'JAVASCRIPT', -text => "var textToShow = \"$data\";", }, { -language => 'JAVASCRIPT', -src => '/data/scripts/char_by_char.js' }, { -language => 'JAVASCRIPT', -src => '/data/scripts/orderlist.js' } ], -style => '/data/scripts/style.css', -title => 'Order Project!' ); __END__ <!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> <title>Order Project!</title> <link rel="stylesheet" type="text/css" href="/data/scripts/style.css" +/> <script type="text/javascript">//<![CDATA[ var textToShow = 'Some text in a variable defined before loading scrip +t files'; //]]></script> <script type="text/javascript"></script> <script src="/data/scripts/char_by_char.js" type="text/javascript"></s +cript> <script src="/data/scripts/orderlist.js" type="text/javascript"></scri +pt> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +" /> </head> <body>

Since your variable textToShow thusly is defined before loading any JavaScript file, It will be available to your functions inside those files - if you don't re-define that variable in your JavaScript file, that is.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: Separate Javascipt code from pure Perl code by shmem
in thread Separate Javascipt code from pure Perl code by Nik

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.