Passing CGI output string into Javascript variable.

S

stahl.karl

I have a CGI/Perl program that returns a string output. Is it possible
to get this into a Javascript variable, where the name of the variable
is defined in the Javascript and not in the Perl code? For example, it
seems that the following should work, but it doesn't appear to. Is
this valid code?


CGI code --- get_string.pl ------

print "Content-type: text/plain\n\n";
print "ahoy matey";
-------------------------------------------


Javascript/html ------------------

<script type="text/javascript">
var mystring = eval('http://blabla/cgi-bin/get_string.pl');
document.write(mystring);


Thanks for your help!
 
R

RobG

I have a CGI/Perl program that returns a string output. Is it possible
to get this into a Javascript variable, where the name of the variable
is defined in the Javascript and not in the Perl code? For example, it
seems that the following should work, but it doesn't appear to. Is
this valid code?

You could just write it to the page in a script element, but I guess you
don't want to do that.

CGI code --- get_string.pl ------

Make that say: get_string.js

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

Serve it as the appropriate type for JavaScript.

print "ahoy matey";

Write the following to get_string.js:

var mystring = 'ahoy matey';


Make sure this script element is placed before the one below:

<script type="text/javascript">
var mystring = eval('http://blabla/cgi-bin/get_string.pl');

Eeek, delete that. eval will try to execute the string as if it were
script - URLs aren't script so the results will not be what you are
expecting.

document.write(mystring);

mystring is defined in get_string.js which has been loaded by the
previous script element and is therefore available for use - the
document.write() statement will write 'ahoy matey' to the page.

If it's in the body, you should see the result.


[...]
 
S

stahl.karl

Thanks for your help. I guess I'm being finicky, but is there no way
of simply returning the string from the CGI script? I'd rather define
the variable name in the Javascript part - that way if I want to change
the variable name, I can just change the Javascript and leave the CGI
untouched. It just seems so inelegant to use language A to write out
code word-for-word in language B.
 
R

RobG

Thanks for your help. I guess I'm being finicky, but is there no way
of simply returning the string from the CGI script? I'd rather define
the variable name in the Javascript part - that way if I want to change
the variable name, I can just change the Javascript and leave the CGI
untouched. It just seems so inelegant to use language A to write out
code word-for-word in language B.


The basis for client-server communication on the web is HTTP, which is
essentially the exchange of text instructions. Those instructions may
include links to binary files or streams, but browsers do not interpret
those - they are either displayed (e.g. GIF, JPEG, PNG images), handed
to a plugin (e.g. PDF, Flash, MPEG) or given to a helper application
(e.g. FlashGet for downloading large files).

The only web-safe way to send instructions to the browser is to put them
into a text file (HTML, script, XML, etc.). If that doesn't suit, use
some other protocol and a plugin or helper application.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top