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

javascript in perl cgi script 1

Status
Not open for further replies.

paulj2

Programmer
Aug 30, 2001
2
US
I have a perl script which works fine. It does some browser detection, then prints some things, followed by a javascript statement which inserts two line breaks that I only want some browsers to see (thus the javascript inside perl).

This works :

print "Content-type: text/html\n\n";

print &quot;<script language=\&quot;javascript\&quot;>\n&quot;;
print 'document.write(\'<br><br>\')';
print '</script>';

This doesn't work, although I can get it to print the code, which I can see with view source, but without doing the line breaks:

print &quot;Content-type: text/html\n\n&quot;;

print &quot;<script language=\&quot;javascript\&quot;>\n&quot;;
print &quot;<\!-- &quot;;
print 'document.write(\'<br><br>\')';
print &quot;// -->&quot;;
print &quot;</script>&quot;;

In other words, I'm trying to include the usual code to hide the javascript from older browsers as opposed to the first example I shown above. What you see here is only my latest attempt. I've tried all sorts of variations - single quotes, double quotes, escaping different characters, etc. Can any of you Perl gurus who also know some javascript shed some light on this?

TIA,
Paul
 
Here you go:

print &quot;Content-type: text/html\n\n&quot;;

print &quot;<script language=\&quot;javascript\&quot;>\n&quot;;
print &quot;<\!--\n&quot;;
print &quot;document.write(\'<br><br>\');\n&quot;;
print &quot;//-->\n&quot;;
print &quot;</script>&quot;;


Chad. ICQ: 54380631
 
Works fine. Thanks, Chad. Don't know why I didn't try this ;-)
 
You don't HAVE to use quotes or apostrophes around strings. Using the 'q' (quoting) operator you can use just about any character you want, then you won't have to escape quotes or apostrophes. You can also use the <<tagname form of print, and you won't have to escape quotes or apostrophes there either. I really like using the 'q' operator, it makes things much simpler. Too bad so few people know about it. Here are two other ways to do it more simply:
Code:
# using the q operator with [] delimiters
print q[Content-type: text/html\n\n];
print q[<script language=&quot;javascript&quot;>\n];
print q[<!--\n];
print q[document.write('<br><br>');\n];
print q[//-->\n];
print q[</script>];
# using print <<tagname
print <<END;
Content-type: text/html

<script language=&quot;javascript&quot;>
<\!--
document.write('<br><br>');
//-->
</script>
END
Note that with the <<tagname form you don't even have to use \n for a linefeed - the normal linefeed at the end of each line it used. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I goofed :~/! And I also missed explaining an important point in the above: q[] works like apostrophes, meaning that NOTHING inside the [] is interpolated, including variables and \n. qq[] works like quotes, so variables and escape sequences WILL be interpolated. The above example should have use qq instead of q!

I also should have made it clearer that you can use almost ANY character as the string delimiter, and some paired delimiters. The four types of brackets will all work: () <> [] {}. If you want to be completely wild, you can even use unprintable characters as the delimiter! For example, this code will work fine, using the ascii control character with hex code 002 as the delimiter:
Code:
print qq\002a string\002;

Here's the full list of the &quot;Quotelike Operators&quot; that you can use in perl. In the list below, {} can be any character of pair of bracketing characters as described above:
Code:
Customary  Generic  Meaning     Interpolates
   ''        q{}    Literal          no
   &quot;&quot;       qq{}    Literal          yes
   ``       qx{}    Command          yes
            qw{}    Word List        no
   //        m{}    Pattern Match    yes
             s{}{}  Substitution     yes
            tr{}{}  Translation      no
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top