Reputation: 217
I have a Perl script which prints out of html page. I want to use javascript to pop alert msg. The alert message is defined as string in the perl variable. I am trying to pass the perl variable value to javascript function as argument but it's not working. Please help.
$perl_variable = "Welcome"; # alert msg
print <<START
<HTML>
some html code....
<p>Click the button to wait 3 seconds, then alert "Hello".</p>
<button onclick="myFunction('$perl_variable')">Try it</button>
<script>
function myFunction(var message){
setTimeout(function(){alert(message)},3000);
}
</script>
</HTML>
START
Upvotes: 3
Views: 5720
Reputation: 944430
NB: The first half of this answer refers to the code that originally (before edits) appeared in the question.
You need to:
Such:
myFunction('$perl_variable')
Note that if your data might include characters that are not allowed in a JavaScript string literal (such as a new line), characters that have special meaning in a string literal (such as the quote mark that delimits it) or characters that have special meaning in HTML (such as the quote mark that delimits the attribute value) then you will also need to perform suitable escaping (in two steps, first for JS, then for HTML).
As an aside, your function definition in JS is also wrong:
function myFunction(var path){
The var
keyword may not be used in the FormalParameterList. That should read:
function myFunction(path){
Upvotes: 6