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

Hi Guys, I'm trying to pass an integer value stored in a Perl variable to a javascript function, and having problems.

Here are a couple of snips of code I'm working on (contained in a Perl library). Assuming that the Perl variable is '$time', where am I going wrong?? Thanks!!

The function call from

<body bgcolor=dcdcdc width=100 onload=callrefresh($time);>
which calls:
function callrefresh(t){ time = t; millisec = parseInt(time*1000); setTimeout('refresh()',millisec); }

Code tags - dvergin 2003-12-11

Replies are listed 'Best First'.
Re: How do you pass a perl variable to a Javascript function
by Roger (Parson) on Dec 12, 2003 at 01:17 UTC
    Can you show us the code you think has the problem? I mean the perl code that prints the <body ... bits. I suspect it's the variable interpolation problem again with single and double quotes for your print statement.

    And please add some <code> ... </code> tags around your code.

      I suspect it is a bit worse than that. Seems like maybe that is just a static html page, that he/she expects to have the value for $time magically placed in.?? That's the impression I got anyway.

      But anyway, as Roger says, you may need to come up with some code and then paste it here.

      Regards
      Gerard
        Thanks for the feedback folks. BTW: My name is David.
        MORE BACKGROUND:
        I have a form in an html page that collects 3 variables. The perl script below uses those variables. I set $try = $FORM_DATA{'fresh'} below, which is the integer variable I want to use in the perl library libprint.pl.
        #!/usr/bin/perl require "libcgi13.pl"; #library from lesson10 require "libprint.pl"; #print/refresh output &parse_input; &print_header; $MAXSAVE = 20; flock("gdata.txt",2); open(FROMFILE,"<gdata.txt"); while(<FROMFILE>){ push(@everyline, $_); } $longstring = join("",@everyline); @oldentries = split(/<!--NEWENTRY-->/,$longstring); if(@oldentries + 1 >= $MAXSAVE){ pop(@oldentries); } close(FROMFILE); open(TOFILE,">gdata.txt"); seek(TOFILE,0,0); print TOFILE "<!--NEWENTRY-->"; print TOFILE "<font face=arial><b> $FORM_DATA{'name'}: </b>"; print TOFILE " $FORM_DATA{'comments'} </font><br>\n"; shift(@oldentries); foreach $entry (@oldentries) { print TOFILE "<!--NEWENTRY-->"; print TOFILE $entry; } truncate(TOFILE, tell(TOFILE)); close(TOFILE); flock("gdata.txt",8); open(TOFILE,">ctime.txt"); print TOFILE " $FORM_DATA{'fresh'}"; close(TOFILE); $try = $FORM_DATA{'fresh'}; &print_entry();
        Now, in libprint.pl (below), I am able to print the integer variable $try (i.e. I know it's there). However, my problem is how do I get a perl variable passed to a javascript function? I've tried passing the value a number of ways but so far I've been unsuccessful.
        #!/usr/bin/perl sub print_entry { open(TIMEFILE,"<ctime.txt"); while(<TIMEFILE>){ push(@time, $_); } close(TIMEFILE); open(FROMFILE,"<gdata.txt"); print <<ENDHEADER; <head><title>D-Spot Chat Line</title> <script language=javascript> function callrefresh(try){ //time = $time[0]; time = try; millisec = parseInt(time*1000); setTimeout('refresh()',millisec); } function refresh(){ this.location.href = "cout.pl"; } </script> </head> <body bgcolor=dcdcdc onload=callrefresh($try);> <center><font face=Tahoma> <h2>Chat Lounge $try </h2> </font> </center> ENDHEADER while (<FROMFILE>) { print; } close(FROMFILE); } return 1;
Re: How do you pass a perl variable to a Javascript function
by punkish (Priest) on Dec 14, 2003 at 00:38 UTC
    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.