Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing variables to a Javascript...

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello, I am having some issues passing variables to a Javascript from a Perl CGI program. What I want is to send back a set of variables to a Javascript program which is to make appropriate alterations to an html document according to the variables which it had been passed....

what I've tried...

-printing out the code from the Perl script as a javascipt
application... I then used the cgi program as a source for the
javascript for the html doc. The problem that arose was that I couldn't figure out how to link to the html doc and pass it the variables instead

of going straight back to the same html doc...

-printing out plain text which stated <script> variables blah blah blah</script> and plugged in an alert call at the beggining of the javascript to watch out for the variables... I just couldn't get this idea to work...

Any solution, new ideas of plain advice is kindly appreciated... thanks
 
If I were you I'd just do it in perl because javascript is very restricted as to file access. I haven't seen any way to open a file/url with javascript, but if someone can show me, I'd be grateful.
In your case, you can use javascript's &quot;location.search&quot; to get the values from the url (If your url is &quot; location.search will return to you &quot;?var=this&var2=that&quot;). I don't know how to open files with javascript, but that will get you the values into the script.
I would just write a perl script that retrieves a query with the specified url and have the perl script open the file and edit whatever and close it. most everything that javascript can do can be done with perl.
Steve Kiehl
webmaster@nanovox.com
 
If you just do

<script language=&quot;Javascript&quot; src=&quot;/cgi-bin/your_perl_script.cgi&quot;>
</script>

You should be able to print any values you want from perl, and they will just appear as Javascript to the browser.

The nice thing about this is that your main page doesn't even need to be a Perl script at all.

Inside your Perl script, let's say, for example, you have a variable named $option1 (let's say it's a text string), and you want to pass the key/value of that variable to Javascript:
Code:
$option1 = &quot;My text string&quot;;
print &quot;option1 = '$option1';&quot;;
Now, what the browser sees as the Javascript source is:
Code:
option1 = 'My text string';

You have to be careful, though. For example, the following would not work:
Code:
$option1 = &quot;My text string\nThe second line of my text string&quot;;
print &quot;option1 = '$option1';&quot;;

because what the browser would see is:
Code:
option1 = 'My text string
The second line of my text string';
which would cause a parse error in Javascript, since there is no string terminator on the first line.

So, when you are passing string variables to Javascript, you need to double-escape your newlines, quotes, etc... like so:
Code:
$option1 = &quot;My text string\\nThe second line of my text string&quot;;
print &quot;option1 = '$option1';&quot;;
 
If you just do

<script language=&quot;Javascript&quot; src=&quot;/cgi-bin/your_perl_script.cgi&quot;>
</script>

You should be able to print any values you want from perl, and they will just appear as Javascript to the browser.

The nice thing about this is that your main page doesn't even need to be a Perl script at all.

Inside your Perl script, let's say, for example, you have a variable named $option1 (let's say it's a text string), and you want to pass the key/value of that variable to Javascript:
Code:
$option1 = &quot;My text string&quot;;
print &quot;option1 = '$option1';&quot;;
Now, what the browser sees as the Javascript source is:
Code:
option1 = 'My text string';

You have to be careful, though. For example, the following would not work:
Code:
$option1 = &quot;My text string\nThe second line of my text string&quot;;
print &quot;option1 = '$option1';&quot;;

because what the browser would see is:
Code:
option1 = 'My text string
The second line of my text string';
which would cause a parse error in Javascript, since there is no string terminator on the first line.

So, when you are passing string variables to Javascript, you need to double-escape your newlines, quotes, etc... like so:
Code:
$option1 = &quot;My text string\\nThe second line of my text string&quot;;
print &quot;option1 = '$option1';&quot;;

Obviously, there are all sorts of thorny issues here about how you interpolate, and how you nest quoting methods inside of Javscript, etc...

The best way to debug this is to run your Perl script output from the command line, take the output and paste it into the <script> section of your HTML file and see if it runs.

If you go to you will find some interesting tools that are aimed at helping developers bridge such gaps between languages, by serializing variables, arrays, hashes, etc... As I understand it, there is a Perl WDDX module available, and there happens to be a Javascript WDDX module also, so this might be your solution for a complicated project.
 
If you are trying to do what I think you are, then you can try this method as well:
Create an iframe and set your cgi script to generate the page in that iframe, for example:
<iframe name=&quot;myframe&quot;></iframe>
<form action=&quot;myscript.cgi&quot; target=&quot;myframe&quot;></form>

Then in myscript.cgi you might have code along the lines of the following:


my $aVariableIWantToPass = &quot;hello&quot;;

print qq~
<head>
<html>
<script language=&quot;javascript&quot;>
function passVars() {
top.variablePassed = $aVariableIWantToPass;
top.doSomethingNow();
}
</script>
</head>
<body onLoad=&quot;passVars()&quot;>
</body>
</html>
~;

when the page loads it is gonna pass &quot;hello&quot; to the javascript variable &quot;variablePassed&quot; on your main page. In addition you top.doSomethingNow() will call that function on your main page telling your javascript to do something now that the variable is passed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top