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 params from Perl to javascript

Status
Not open for further replies.

jez

Programmer
Apr 24, 2001
370
VN
Hi there,

I have set of scripts where I need to generate HTML pages with Perl (no problem so far)...but in these pages I am providing links to other generated pages. (still no problem).

Except on one page that is a 3 frame frameset, I am using a javascript function i wrote to change all 3 frames at once to link to another frameset with a different main frame content, (to make it look like the frameset has changed).

But although i wrote this bit javascript and it works when using static pages, it doesn't with these dynamic pages.

Part of the reason is that I need to pass variables from my perl script to links in my generated page that call the javascript - so that the javascript can then add these variables to the links it executes as parameters for the script that they're linking to. - yeah it seems like these values are really going the long way round to get to the target script, but because of the structure and layout I can't really control that.

BUT I stripped down my scripts to find the error, and I have found that if I do exactly as I am trying to do but making sure that the value I pass to my javascript is an integer (i.e. 3, 28, etc) it works...but my real values are strings (i.e. P002, P003, P004...etc), and these don't make it to the javascript function. When I mouse over the links to the javascript function, these string values have been interpolated by perl (as they are the result of a loop), but then I get errors when calling the javascript.

Are there known issues with this kind of thing or am I just doing it wrong.

Any help would be great.

Thanks

Jez

:)
 
I have figured this out now, I'm not sure what i was doing wrong but I'm not anymore;-

My script was making links like so;-

foreach $prop(@props) {
print &quot;<a href=javascript:con('$prop');>$prop</a> &quot;;

and the javascript function con is as follows;-

function con(j){
//alert(j);
top.main.location.href = &quot;modo2.cgi?pno=&quot; + j;
top.topone.location.href = &quot;modoctop.htm&quot;;
top.left.location.href = &quot;modo3.cgi&quot;;}


For several hours before posting this issue here, I just couldn't get the alert of 'j' to appear, it does now and all i can see as changed is the number of ' and &quot; I have used while writing the HTML from the perl script...these can always trip you up as they have to conform to many different situations (i.e. HTML, javascript, perl etc..).

HTML is very forgiving so I can get away with not quoting the javascript call in my links, if this wasn't the case I don't think this would have worked.


Jez

:):):)
 
These cases are always a question of what the actual output looks like to the browser, because that's all the browser has to go on; it doesn't know whether the text it receives was generated by Perl or ASP.

I would recommend that you use the &quot;here-docs&quot; approach to outputting Javascript from Perl, becase then you don't have to worry about escaping quotes, generating newlines, etc...:

Code:
print <<EOF;
<script language=&quot;Javascript&quot;>

myjsvariable = &quot;$perlvariable&quot;
mynextjsvariable = &quot;$nextperlvariable&quot;

//rest of script....

</script>
EOF

If you're not familiar with this method, it is a common Perl approach to outputting a block of text with variable interpolation, The great thing here is that there is no use of quotes as delimiters, since the delimiter is the upper-case string &quot;EOF&quot;, and the end is marked by EOF on a line by itself, with no surrounding quotes or whitespace. This makes it easier to make sure that what you are outputting is exactly what the browser will see. You can use any string you want as the delimiter, in case there is a problem with using EOF.
 
Yeah, I have been using those and they do make it alot more straight forward, but it was outside of one of these where i was looping through data output where I tripped up.

But I suppose you can use a here document with a loop?

e.g. (and I'm just thinking out loud)

foreach $file(@dirlist) {
print<<EOF;
<a href=&quot;$mainpath$file&quot; target=&quot;main&quot;>$file</a>
EOF
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top