By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

Passing CGI output string into Javascript variable.

P: n/a
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!

Jan 17 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
st********@gmail.com wrote:
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';

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

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

<script type="text/javascript" src="get_string.js"></script>

<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.
[...]
--
Rob
Jan 17 '06 #2

P: n/a
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.

Jan 17 '06 #3

P: n/a
st********@gmail.com wrote:
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.
--
Rob
Jan 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.