473,725 Members | 1,942 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 6794
st********@gmai l.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********@gmai l.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
8328
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. any ideas??
1
8423
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
5890
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
5722
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 the code i'm using: <head> <script language='JavaScript'>
1
2195
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
5879
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 some reason the sendPhp is not working. I need to send
1
3588
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
2742
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 catg var f1=document.forms; var sbox1=f1.elements;
2
3342
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 function assigntask(assignedperson,userid) { alert(userid); var test = <? mysql_query("update mytable set assignedto='test454' where id='+userid+'");?> } reply me ASAP
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9174
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4517
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.