///Running a CGI Script When Page Loads

Running a CGI Script When Page Loads

Running a CGI Script When Page Loads

From statistics gathering to silent webmaster notification to email subscription solicitation, there can be any of many reasons for triggering a CGI script to run when a web page loads.

The idea is to make the output of the CGI program an element of the web page. Web page elements are such things as images and external CSS and JavaScript files.

To specify the URL of a CGI program in an IMG tag, in lieu of a real image file, the CGI program will need to return an image to the browser before it finishes running.

Before and after the image is returned to the browser, the CGI program can be updating a log file, setting a cookie, sending an email to the webmaster, and anything else it is supposed to do.

Similarly, JavaScript, CSS, or other page elements can be used to trigger a CGI program. The CGI program then returns data in the format the browser expects.

Complete “How to” instructions are linked from http://willmaster.com/cgionload and contain working CGI scripts that return an image, JavaScript code, and an HTML web page. You can read abut five different methods of running a CGI program when a web page loads.

Below is a synopsis of each method.

It is important to remember that when a CGI program is used in lieu of an image, JavaScript, CSS, or other type of page element, the script must return that type of element to the browser before the script exits. Otherwise, the browser may present a broken image icon or an error message to the site visitor.

“OnLoad” Attribute Activates CGI Script

This method uses the “onLoad” attribute in the BODY tag to launch some JavaScript that runs a CGI program.

The JavaScript creates an image object then loads the SRC of the image object with an image. In this case, the image is provided by the CGI program.

http://willmaster.com/onloadcgi

Automatic Form Submission to a CGI Program

JavaScript can be used to automatically submit a form to a CGI program. This can be done when a page is loaded.

If only CGI environment variables need to be logged and/or cookies set, the form can be submitted without sending regular form data to the CGI program. Otherwise, JavaScript can be used to get information into the form before it’s automatically submitted.

The implementation guidelines contain code to insert the current web page URL and the user’s time zone information into the form.

http://willmaster.com/autoformsubmit

Image Tag Launches CGI Program

As mentioned above, an image tag can be used to launch a CGI program. Instead of an image file, the SRC of an IMG tag contains the URL of the program.

When the CGI program runs, it will, of course, returns an image to the browser before the program completes its tasks.

http://willmaster.com/imagecgi

Auto-Launch CGI Script with JavaScript Tag

JavaScript that retrieves an external file can also be used to run a CGI program. Instead of a JavaScript file, the SRC of an SCRIPT tag contains the URL of the program.

Valid JavaScript code is returned to the browser before the program completes its tasks.

http://willmaster.com/jscgi

IFRAME Tag Will Auto-Launch CGI Script

An IFRAME may be used to automatically launch a CGI script.

Any method discussed above may be incorporated into the web page being loaded into the IFRAME.

Alternatively, the SRC of the IFRAME can contain the URL of a CGI program instead of the URL to a normal HTML web page.

The CGI program can do whatever duties it is programmed to do and, before quitting, return an actual HTML web page for the browser to put into the IFRAME.

http://willmaster.com/iframecgi

2010-05-26T11:31:04+00:00 April 21st, 2005|CGI and Perl|0 Comments

About the Author:

Leave A Comment