Question

Let's say I have a variable called $taskid defined in <%init>. Can I generate a JavaScript function from that variable?

<script language="javascript">

window.onload = function() {
   alert("<% $taskid %>");
};

</script>


<%init>

my $taskid=1;

</%init>
Was it helpful?

Solution

This worked:

<script language="javascript">        
    window.onload = function() {
       alert("<% $taskid %>");
    };        
</script>

<%init>        
    my $taskid=1;
</%init>

OTHER TIPS

Is it possible to initialize a JavaScript variable from Perl?

Since you asked that three times even though it has nothing to do with what you posted, I'll answer it.

No.

  1. Languages can't take actions, much less initialize. A Perl program cannot do so either. A process cannot access variables a) in a different virtual machine, b) in a different process, c) on a different machine. All three apply here.

  2. JavaScript variables can only be initialized by JavaScript assignments. You would need to either

    1. have a JS expression that somehow communicated with a Perl process (e.g. AJAX), or
    2. generate the executed JavaScript code as you showed.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top