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/