471,090 Members | 1,277 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,090 software developers and data experts.

Assigning a CGI output to JavaScript variable.

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
Jul 20 '05 #1
1 8102


sams wrote:
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>


You could use
<script type="text/javascript" src="/cgi-bin/whatever.cgi"></script>
and then let that CGI program generate
var myDate = "...data goes here...";
If you want to read in the output of someone's CGI program then in
general that is not doable as the same origin policy doesn't allow
access to another server.
If you have need to read in any page from your own server then some
browsers like IE5+/Win or Mozilla/Netscape 6/7 allow you to send a HTTP
request and receive the response including the response text. It doesn't
matter if you request a static HTML page or a CGI. For instance with
Mozilla you can do

var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'whatever.cgi', false);
httpRequest.send(null);
alert(httpRequest.responseText)

Note that the above requests the resource synchronously, meaning the
script waits for the response and the browser is blocked while doing the
request so you should do it asynchronously if you want the browser to
stay responsive:

var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'phpinfo.php', true);
httpRequest.onreadystatechange = function (evt) {
if (httpRequest.readyState == 4) {
alert(httpRequest.responseText);
}
};
httpRequest.send(null);
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Sandman | last post: by
37 posts views Thread by miken32 | last post: by
7 posts views Thread by jodleren | last post: by

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.