473,508 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing CGI output string into Javascript variable.

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
3 6758
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8318
by: Matt | last post by:
If I assign VBScript server side variable a to javascript variable x, it is fine. <% Dim a, b a = 10 %> var x = <%= a %>; alert(x); But if I do the other way around, then it has 500 error....
1
8410
by: sams | last post by:
How can I assign a return value of a CGI to a JavaScript variable? Or what is the correct way? <script> var myData = eval(http://somewhere/cgi-bin/data.cgi"); </script> TIA Sam
1
5881
by: Timmy | last post by:
Hi, Is there anyway to read an html file into a javascript variable or execute a script src and load that output into a variable in the calling page? Thanks, Tim
3
5697
by: mbasil7 | last post by:
Hi at all! I want to use a javascript variable in php. The reason is that i want to know the client's screen resolution. Keep in mind that i am not a javascript programmer but php. Here is...
1
2185
by: Steve Cosh | last post by:
Hi, is there any way of passing a javascript variable over to a asp variable so i can write it to my database. Thanks
2
5873
by: Geoff Cox | last post by:
Hello, The code below is aimed at passing the date in the yyyyMMdd format from the javascript calendar in an html file to the php in a another file which then searches a MySQL database. For...
1
3557
by: Ranjan | last post by:
Hi, I want to assign javaScript variable to JSP variable. <Script> var conf=Confirm("Mess"); if(conf) { <% String str=conf; /// line 1 %> }
5
2713
by: nbt725 | last post by:
Dear Sir, Hello ! I want to get the javascript variable in php code in following function. <script language="JavaScript"> function chg_subcatg(cfield,fieldnm) { // For Retrieval of current...
2
3330
by: yuva17raj | last post by:
hi i need to write a javascript function in which i need to update the record by passing a javascript variable and i have tried in this format but no solution can any one help me for the following...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.