1

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>
3
  • Is it possible to initialize a Javascript varable from Perl? Commented Oct 31, 2013 at 18:43
  • Mason processes the data before it is sent through the http server to the client. Therefore, you can even generate whole javascript functions in otherwise static html from within mason. Otherwise: if the html is on the client - and the javascript does one or another thing there, this NOW cannot be affected by mason because it NOW does not originate from the http server. Commented Oct 31, 2013 at 18:47
  • 1
    In the time it took you to post this question you could have just tried running that Mason code to see what happens. Commented Oct 31, 2013 at 18:53

2 Answers 2

1

This worked:

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

<%init>        
    my $taskid=1;
</%init>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.