Game Development Community

Calling Perl CGI from JavaScript

by David Wyand · in Technical Issues · 07/07/2002 (3:22 pm) · 5 replies

Greetings!

Here's what I'd like to do:

After a web page loads up, I'd like call a Perl CGI program that will return the path to a picture. This new picture path will then be used to change the source of an existing picture on a page.

Here's the reason behind it:

I have a Perl CGI function that may take a few seconds to run, and like it to not block the loading of the rest of the page. If I use the CGI script as a server side include, it appears that the rest of the web page will not be displayed by the web browser until the script returns (which makes sense to me). I'd like the whole page to load, and then based on the CGI script, change the display of an image.

I've managed to use an 'onLoad' event to allow me to change an image's source after the web page is loaded. However, I've not been able to make this new source dynamic based on what is returned from the CGI script.

My question is this:

Is there a method to call a Perl CGI script within JavaScript that will allow me to store the results (from the STDOUT of the CGI script) into a JavaScraipt variable? Something along the lines of:

var newimage = executeThisURL("cgi-bin/myscript.cgi");
myimage.src = newimage;

I know one possible answer is to just use frames, but I'm trying to avoid them as I just don't like them.

After searching for this on the internet and going through my JavaScript reference, I'm stumped.

- LightWave Dave

About the author

A long time Associate of the GarageGames' community and author of the Torque 3D Game Development Cookbook. Buy it today from Packt Publishing!


#1
07/07/2002 (4:51 pm)
Do all the pictures you might display exist on the local system (well, the HTTP server the Perl CGI is running on, not the end client), or might they be references to off-site images?

The reason I ask is that IMO the ideal way to handle this situation would be for the Perl CGI to actually serve the image rather than just a reference to it, so you could just do on the client side and then have the Perl CGI program actually spit back a valid GIF, JPG, PNG, or whatever as opposed to spitting back a URL. This shouldn't halt the general page rendering and saves you from dealing with the minor javascript differences that invariably pop up between different browser versions.


In reference to your question the way you asked it though, I'm afraid my JavaScript/DHTML is too rusty to give you exact pointers though I believe what you want to do is certainly possible, at least with 4.0+ browsers.
#2
07/07/2002 (7:16 pm)
Greetings!

Yes, the images are local to the server and Perl CGI.

I believe I understand what you are referring to. By having the Perl CGI 'serve' the image, I take it you mean that it actually sends the binary info for the picture. Thanks for suggesting that. I'll have to give it a try.

- LightWave Dave
#3
07/08/2002 (8:14 am)
Adding width/height attributes to an img tag will make the web browser load _and display_ the rest of the page properly. Great if you've got a fixed size graph or something...
#4
07/08/2002 (10:16 am)
Adding width/height tags is definately a good thing if you know them beforehand, but if I understand the original problem correctly the server is blocking on a (sometimes lengthy) call to a Perl CGI that is required to complete the basic page text (because it is looking up the URL to a picture). In this case, width/height tags won't really make a noticable difference because the browser is going to be sitting there waiting for the rest of the initial HTML anyway.

And yes, David, you have the right idea of what I was talking about. As long as your HTTP headers that the Perl CGI sends back denote the right file-type mappings, browsers should properly render it as an image even if the SRC to it is "whatever.cgi?somearguments", eg:



If all the images are the same size or there's a quick way to determine the image size based on some lookup, I would recommend you put width/height in that IMG tag so the page doesn't have to 'adjust' itself after the graphic loads, but I believe this technique should work to solve the basic blocking problem you described.
#5
07/08/2002 (1:18 pm)
Greetings!

Thanks again for the info. All the images are indeed the same size, so I don't have to worry about a page reformat once the image is actually displayed.

I've never had a Perl CGI script serve an image before, so that's what I'll need to play with. Here's what I would think the steps are:

1. Send out the correct HTTP header for my GIF images. Something like:

print "Content-type: image/gif\r\n\r\n";
2. Read in the GIF image in binary format, and send it out byte-by-byte.

Hmm, that seems like that's it. Off I go...

- LightWave Dave