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

While trying to make an interactive page mixing perl and javascript I have reached a problem. The problem is how to pass information to Javascript from Perl. I am using Perl to read a MySQL database to get information to populate the form with. This will eventually likely end up in an Oracle Database but I am guessing the issue won't change. In order to do some of the work I need to pass information so that the Javascript functions and such can read them. Is my only real option using hidden fields?

  • Comment on How do I send information to Javascript from perl?

Replies are listed 'Best First'.
Re: How do I send information to Javascript from perl?
by chip (Curate) on Jan 01, 2002 at 02:35 UTC
    You probably just need to interpolate the Perl values into the JavaScript code as it's sent.

    For example, if "foo" is a value you want the JS code to see, your program should at some point do something like this in the midst of transmitting JS code:

    print "foo = '\Q$foo\E'\n";

    NOTE: Using \Q should protect against most metacharacters in the variable, but you should be quite careful to determine if it does what's appropriate for JS quoting conventions.

        -- Chip Salzenberg, Free-Floating Agent of Chaos

      one approach would be to redirect to an html file with a query string after you've done all your processing in your perl such as:
      $username = 'username'; print "Location: html.html?username=$username\n\n";
      from there, you can simply grab the variable on the html using this:
      <script language="JavaScript"><!-- if (location.search.length > 0) eval('var ' + location.search.substring(1)); //--></script>