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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem incorporating JavaScript in Perl 1

Status
Not open for further replies.

Albuckj2

Programmer
Jan 7, 2003
68
GB

My Perl/CGI program is producing an error, for no apparent reason (although there must be an obscure one!)
The following piece of code is the problem, as the page prints out "Alright NOW1", then the error appears, and then shows "Alright NOW2" along with the rest of my html (which is fine). But anything on the page that uses the piece of JavaScript below fails. Also, I put an alert in the JavaScript, but this doesn't come up at all.

printf &quot;<html>&quot;;
printf &quot;<head><title> table </title>&quot;;
printf &quot;Alright NOW1!!!&quot;;
printf &quot;<SCRIPT LANGUAGE='JavaScript'>;&quot;;
printf &quot;alert('It is now in the JavaScript');&quot;;
printf &quot;var temp = 'inbox';&quot;;
printf &quot;var temp2 = 'box2';&quot;;
printf &quot;var temp3 = 'trash';&quot;;
printf &quot;function goto(to)&quot;;
printf &quot;{ document.mailform.box.value = to; document.mailform.submit(); }&quot;;
printf &quot;function delete()&quot;;
printf &quot;{ document.mailform.delmove.value = 'del'; document.mailform.submit(); }&quot;;
printf &quot;</script>&quot;;
printf &quot;</head>&quot;;
printf &quot;Alright NOW2!!!&quot;;
printf &quot;<body>&quot;;

The error porduced is below:

A runtime error has occured.
Do you wish to debug?

Line: 0
Error: Expected '('

Any ideas??? Desperately need some help......

 
Hi,

I don't know a lot about JavaScript, but I believe that your problem is caused by the delete() function you have created.

I think 'delete' is a JavaScript command (certainly trying &quot;delete(hello);&quot; appears to work) and Java thinks you are trying to use this command rather than defining a function. You would think it would be clever enough to spot what you are trying to do and warn you that it is an invalid function name, but evidently it does not.

I tried changing that function to 'delete2()' and it appeared to work fine.

Also I don't think you need the semi colon after the '>' in in the line:

printf &quot;<SCRIPT LANGUAGE='JavaScript'>;&quot;;

as this is an html tag not javascript.

Incidentally, why are you using printf? That's generally used for formatting data, such as forcing numbers to have 2 decimal places for currencies (eg: printf(&quot;%02d&quot;,2); will output 02 as the pattern %02d indicates 'at least two decimal digits'). Just a regular 'print' should work fine.

Hope this helps,

Tyger.

 
You may need to escape the specials characters like ; using a
Like this:

printf &quot;<html>&quot;;
printf &quot;<head><title> table </title>&quot;;
printf &quot;Alright NOW1!!!&quot;;
printf &quot;<SCRIPT LANGUAGE='JavaScript'>\;&quot;;
printf &quot;alert('It is now in the JavaScript')\;&quot;;
printf &quot;var temp = 'inbox'\;&quot;;
printf &quot;var temp2 = 'box2'\;&quot;;
printf &quot;var temp3 = 'trash'\;&quot;;
printf &quot;function goto(to)&quot;;
printf &quot;{ document.mailform.box.value = to\; document.mailform.submit()\; }&quot;;
printf &quot;function delete()&quot;;
printf &quot;{ document.mailform.delmove.value = 'del'\; document.mailform.submit()\; }&quot;;
printf &quot;</script>&quot;;
printf &quot;</head>&quot;;
printf &quot;Alright NOW2!!!&quot;;
printf &quot;<body>&quot;;

I am not sure, but you may even need to user \ to escape the ()'s as well.

Hope this helps!
 
Howdy...

bpinos is correct in general… Because it is at line 0 it looks like your losing the bracket right after alert… escape all your brackets. You are likely loosing more than that, but because the actual Perl parsed code is not shown, I can’t tell.

Parsing JavaScript with Perl can get tricky. There are allot of common operators and such between Perl and Javascript. It is a good rule to escape as many of those common characters as you can if you have to have the code parsed by Perl.

- Try to call javascripts from outside your parsed page <SCRIPT LANGUAGE=\”JavaScript src=\”myscript.js\”> This can save allot of head aches.

- Where I must parse JavaScript I use a simple trick to determine if it is parsing correctly; View the source of the parsed code in your browser and compare it to the original un-parsed code (local). It’s very likely you'll loose a piece of the code when parsed, if not escaped.

Good Luck.
 
Er. You don't want to be using [tt]printf[/tt], you want to be using [tt]print[/tt]

[tt]print &quot;<html>&quot;;[/tt]

[tt]printf[/tt] is for printing formatted strings, and needs to take a format string followed by list of variables, like C [tt]printf[/tt]
 
Cheers for the help guys.
Removing the delete function solved it. However, I need the script to work in Netscape Navigator, and for some reason the javascript doesn't work - I click on one of the icons on the left (&quot;Inbox&quot;, &quot;Box2&quot; or &quot;Trash&quot;), and it does nothing, whereas in IE the function &quot;GoTo&quot; is called and an alert box is shown (followed by submission of the form).

Someone suggested putting <!-- and --> round my javascript code, but this caused it not to work in either IE or NN!

The code is below. Thanks for any help.

print &quot;<html>&quot;;
print &quot;<head><title> Webmail table </title>&quot;;
print &quot;<SCRIPT LANGUAGE='JavaScript'>&quot;;
#print &quot;<!--&quot;;
print &quot;var temp = 'inbox';&quot;;
print &quot;var temp2 = 'box2';&quot;;
print &quot;var temp3 = 'trash';&quot;;
print &quot;function goto(to)&quot;;
print &quot;{ alert('get there'); document.mailform.box.value = to; document.mailform.submit(); }&quot;;
print &quot;function del()&quot;;
print &quot;{ document.mailform.delmove.value = 'del'; document.mailform.submit(); }&quot;;
#print &quot; -->&quot;;
print &quot;</script>&quot;;
print &quot;</head>&quot;;
print &quot;<body>&quot;;
print &quot;<form name='mailform' action='/~cs91aj/mail.cgi' method='Post'>&quot;;
print &quot;<input type='hidden' name='name' value='yes'>&quot;;
print &quot;<input type='hidden' name='pass' value='yes'>&quot;;
print &quot;<input type='hidden' name='box' value='$box'>&quot;;
print &quot;<input type='hidden' name='delmove' value='none'>&quot;;
print &quot;<table border=0><tr><td valign='top'>&quot;;
print &quot;<table border=0>&quot;;
print &quot;<tr><td><a href='javascript:goto(temp);'>Inbox</a></td></tr>&quot;;
print &quot;<tr><td><a href='javascript:goto(temp2);'>Box2</a></td></tr>&quot;;
print &quot;<tr><td><a href='javascript:goto(temp3);'>Trash</a></td></tr>&quot;;
print &quot;</table></td><td>&quot;;

etc....
 
Check out the huge array of quote and quote-like operators available in perl. It is usually possible to write elegant and legible code without the flurry of backslashes that can infest other languages. For starters, &quot; and qq{ interpolate whereas ' and q{ don't. Use ' or q{ unless you want interpolation and life gets easier.

My first re-write would be something like
[tt]print q{<html>
<head><title> Webmail table </title>
<SCRIPT LANGUAGE='JavaScript'>

var temp = 'inbox';
var temp2 = 'box2';
var temp3 = 'trash';

function goto(to) {
alert('get there');
document.mailform.box.value = to;
document.mailform.submit();
}

function del() {
document.mailform.delmove.value = 'del';
document.mailform.submit();
}
</script>
</head>

<body>
<form name='mailform' action='/~cs91aj/mail.cgi' method='Post'>
<input type='hidden' name='name' value='yes'>
<input type='hidden' name='pass' value='yes'>
<input type='hidden' name='box' value='$box'>
<input type='hidden' name='delmove' value='none'>
<table border=0><tr><td valign='top'>
<table border=0>
<tr><td><a href='javascript:goto(temp);'>Inbox</a></td></tr>
<tr><td><a href='javascript:goto(temp2);'>Box2</a></td></tr>
<tr><td><a href='javascript:goto(temp3);'>Trash</a></td></tr>
</table></td><td>
};
[/tt]

which adds legibility (and can save many minutes of problem-solving).

The DBI utility, however, makes this sort of coding even easier. Consider:

[tt]use CGI qw/ :standard :html3 :shortcuts form/;

my $js = q{
var temp = 'inbox';
var temp2 = 'box2';
var temp3 = 'trash';

function goto(to) {
alert('get there');
document.mailform.box.value = to;
document.mailform.submit();
}

function del() {
document.mailform.delmove.value = 'del';
document.mailform.submit();
}
};

print header,
start_html( -title=>'Webmail table', -script=>$js ),
form( {-name=>'mailform', -action=>'/~cs91aj/mail.cgi'},
hidden( name=>'name', value=>'yes' ),
hidden( name=>'pass', value=>'yes' ),
hidden( name=>'box', value=>'$box' ),
hidden( name=>'delmove', value=>'none' ),
table( {-border=>0},
Tr(
td( {-valign=>'top'},
table( {-border=>0},
Tr(
td( [
a( {-href=>'javascript:goto(temp);'}, 'Inbox' ),
a( {-href=>'javascript:goto(temp2);'}, 'Box2' ),

a( {-href=>'javascript:goto(temp3);'}, 'Trash' ),
] ),
),
), #end table
),
),
),
),
end_html;
[/tt]

I think it's prettier!

&quot;As soon as we started programming, we found to our surprise that it wasn't as
easy to get programs right as we had thought. Debugging had to be discovered.
I can remember the exact instant when I realized that a large part of my life
from then on was going to be spent in finding mistakes in my own programs.&quot;
--Maurice Wilk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top