numele
April 12, 2010, 5:56pm
1
How do I pass a bash variable to a javascript?
I've tried
#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "<html>"
echo "<head>"
counter=0
echo '<script>
window.parent.document.forms[0].counter.value = "$counter";
</script>'
I have an iframe script which I am trying to pass a counter to the parent. All I get to display on the parent page is:
$counter
I've tried many variations for the varible like:
\'$counter\'
[ "$counter" ]
("$counter")
Scott
April 12, 2010, 6:16pm
2
Did you try putting the echo in double quotes? i.e.
$ cat JS1
counter=3
echo "<script>
window.parent.document.forms[0].counter.value = '$counter';
</script>"
$ ./JS1
<script>
window.parent.document.forms[0].counter.value = 3;
</script>
http headers are supposed to be \r\n terminated, not just \n (which is what your echo statements are doing). Probably unrelated to your javascript question, but it may save you debugging time.
numele
April 12, 2010, 6:38pm
4
Thank you scottn, works like a charm. Also, thanks alister for pointing out my html mistake.