Forum Moderators: coopster & phranque

Message Too Old, No Replies

Using JS to include output of CGI

Format for output?

         

molecularr

6:19 pm on Nov 11, 2004 (gmt 0)

10+ Year Member



Hello,

This may be a question for the Javascript forum, but I think my problem lies on the CGI side of things. I'm trying to include dynamic information in a page that's already dynamically generated by a cgi (a different one).

I've heard of a technique whereby you use javascript to include the output of a script, so I'm basically inserting this javascript include into the template that the main CGI fills in. The problem is, it doesn't work.

Here's what I put in the page.

<script src="url/test/hello.cgi" language="text/javascript"></script>

The idea is that 'hello.cgi' will output html through a series of 'document.write()' statements that the browser will print as html.

Here's my hello.cgi:

#!/usr/local/bin/perl 
print "Content-type:application/x-javascript\n\n";
print "document.write(\"hello there\")\n\n";
exit;

Basically nothing happens when I call the html file with the js including. When I point directly at hello.cgi, though, it says 'document.write(/"hello there\")'.

I suspect this is a problem with the way Perl is outputting, but I really don't know what to do. Has anyone had experience with this technique?

Thanks for any suggestions/comments/better ideas.

Zach

moltar

5:25 am on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The following code works perfectly for me:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title></title>
<script type="text/javascript" src="http://path_to/js.pl"></script>
</head>
<body>
</body>
</html>

Tip: you can use

print qq$document.write("hello there")$;
to avoid having to escape all the quotes. You can use pretty much any other ASCII character instead of $ sign as long as they are paired.

molecularr

8:01 pm on Nov 13, 2004 (gmt 0)

10+ Year Member



Hmm. Okay, it seems to work now.

I tried your html and it still didn't work, but then I tried making CGI print like you suggested and it worked. I guess the escaped quotes were not being read in the JS.

Thanks, Moltar.