The key to this is to be clear that Perl is happening on the server, while Javascript is happening on the client. There is really no connection between the two. So, on the server you use Perl to create your webpage, which just happens to also build the Javascript code, including any variables that you might be using to build the code, the page and everything else.

Once all this is done, the webpage is generated and sent off to the client (the user's browser). At that point it is any normal webpage, and as far as the user is concerned, anything or any language could have created it. Now, the Javascript works as in any other page.

You can do the reverse and have Js "communicate" with Perl -- use Js to populate form or url variables to send them back to the server so Perl can now use it to do other things.

If you are using the above concept and it is not working, perhaps it would help to post any error messages you might have in the error log. Perhaps the Perl script is breaking elsewhere and not even generating the value that you are expecting in your webpage.


In reply to Re: How do you pass a perl variable to a Javascript function by punkish
in thread How do you pass a perl variable to a Javascript function by Anonymous Monk

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.