jas7
jas7

Reputation: 3075

Using Perl mason variables in Javascript

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>

Upvotes: 1

Views: 1844

Answers (2)

jas7
jas7

Reputation: 3075

This worked:

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

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

Upvotes: 1

ikegami
ikegami

Reputation: 385764

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.

Upvotes: 0

Related Questions