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

Why does a string need to escaped TWICE?

Status
Not open for further replies.

lcs01

Programmer
Aug 2, 2006
182
US
Following is a piece of code mixed with perl and javascript:

Code:
# some perl codes here
my $words = "It's a test";
$words = escape($words); [b]# escape once![/b]
my $jscode =<<BLAARG;
// It does not make a difference using double or single quotes in the folowing line
var words = escape("$words"); [b]// escape twice!![/b]

function myfunction() {
// some js codes here
var url = '/myperl.pl&words=' + words;
var name = 'newWin';
open(url, name);
}
BLAARG

If I omitted 'escape()' once either in perl or js code, then I would get a run time javascript error:

Error: Expected ';'

To me, I would think it should be sufficient to escape once in the perl code.

Could someone explain it to me why? Many thanks.
 
Could you let me know in what context you are doing your code above? You appear to be mixing it with PHP as well?

If you can provide something a little more structured, I'm sure you'd see some assistance.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Do this
Code:
# some perl codes here
my $words = "It's a test";
$words = escape($words);
my $jscode =<<BLAARG;
// It does not make a difference using double or single quotes in the folowing line
var words = [b]"$words";[/b]

function myfunction() {
// some js codes here
var url = '/myperl.pl&words=' + words;
var name = 'newWin';
open(url, name);
}
BLAARG

M. Brooks
 
To Jeff,

No, it is not a PHP question, it is a mix of perl and javascript. What I don't understand is why a string '$words' has to be escaped twice? To me it should be sufficient to escape it in the perl code. But why javascript has to escape it one more time?

To M. Brooks,

I tried your solution before I post my question here. Unfortunately, it did not work. I can not expalin it. I would expect your solution work. But, it does not.
 
The escape() does not exist in perl. Regardless, this works.
Code:
#!/usr/bin/perl

use strict;

my $words = "It's a test";
   $words =~ s/\'/\\'/g;

print "content-type: text/html\n\n";
print <<EOF;
<html>

<script type="text/javascript">
<!--//
var words = "$words";

function myfunction() {
    var url  = '/myperl.pl&words=' + words;
    var name = 'newWin';

    // open(url, name);
    alert(url);
}
//-->
</script>

<body onLoad="myfunction()">

Test

</body>
</html>
EOF

M. Brooks
 
Thank you, M. Brooks.

The perl library method escape() is defined in CGI.pm. I should have mentioned that before.

I'll test your implementation later and will update this thread accordingly.

BTW, the value of '$word' is a user input, which will be used for a full text search. After the string substitution, would the full text search still work? Let me do some tests first.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top