Running a CGI Program On Page Load
From statistics gathering to silent webmaster notification to email subscription solicitation, there can be any of many reasons for running a CGI program "On Page Load".
Automatically Running CGI Programs When Web Pages Load
CGI is unlike Server Side Includes. SSI tags are parsed and content inserted by the server before the page is sent to the browser.
CGI is unlike JavaScript. JavaScript is run within the web page after the page is received from the server.
CGI is a gateway between browser and server. CGI can be used by the browser for sending data to the server and receiving data back.
It's two-way. If it's only one way, only to the server or only from the server, then it's not CGI.
Most web surfers are familiar with forms a form is filled in, the button is clicked, and they get a "thank you" page. If the form is processed with a CGI program:
- The browser sends the form data to the program on the server.
- The program sends data back from the server telling the browser what page or information to display.
Ways to Run a Script When a Page Loads
There are a good number of methods one can use to run a CGI program when a browser loads a web page. This article addresses some of the easiest to implement.
These can be
- an automatically submitted form
- or any method that causes the browser to obtain a page element from a CGI program.
A page element is an image, an external JavaScript or CSS file, or anything else that the browser must fetch in order to render the page.
If the URL or SRC of the page element is to a CGI program, the CGI program will run.
Each method discussed in this article has a link to another article containing specific implementation details.
'OnLoad' Attribute Activates CGI Script
The JavaScript "onLoad" attribute in the BODY tag can be used to automatically run a CGI program when a web page has loaded. Strange, but true. The onLoad attribute runs some JavaScript which runs the CGI script.
See this article for the steps to implement this method.
Automatic Form Submission to a CGI Program
JavaScript can be used to automatically submit a form to a CGI program when a page is loaded.
If you only want to log CGI environment variables, and/or set a cookie, the form can be submitted without sending regular form data to the CGI program. Otherwise, somehow information needs to get into the form before it's automatically submitted.
The implementation details are outlined here.
Image Tag Launches CGI Program
Instead of an image file, the SRC of an IMG tag may contain the URL of a CGI program. The URL may send information to the CGI program when the browser requests the image. Information for the URL can be provided by JavaScript, including visitor's time zone and/or cookie contents.
When the CGI program runs, it will, of course, returns an image to the browser before the program completes its tasks.
This page provides detailed instructions.
Non-Display Image Silently Causes CGI Program to Run
JavaScript code to specify an image can be used to automatically launch a CGI program. No HTML code is used to implement this method.
This method allows programmers to not only launch a CGI program when a page loads, but also when other events happen, like link clicks.
Click here for step-by-step instructions.
Auto-Launch CGI Script with JavaScript Tag
There is a method of using JavaScript to import an external file of JavaScript code and run that code.
Instead of a JavaScript file, the SRC of an SCRIPT tag may contain the URL of a CGI program. Similar to the "Running a CGI Program with an Image Tag" section above, the URL may send information to the CGI program when the browser requests the external JavaScript file.
When the CGI program runs it will of course return valid JavaScript code to the browser before the program completes its tasks.
This articles shows how to accomplish this.
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. Or, instead of the URL of a normal HTML web page, the IFRAME may contain the URL of a CGI program. When the IFRAME is loaded, the script runs.
Click here for step-by-step instructions.
A note about the CGI program these methods interact with
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.
Example CGI scripts can be found in the specific articles describing each implementation.
- 'OnLoad' Attribute Activates CGI Script
- Automatic Form Submission to a CGI Program
- Image Tag Launches CGI Program
- Auto-Launch CGI Script with JavaScript Tag
- IFRAME Tag Will Auto-Launch CGI Script
- Non-Display Image Silently Causes CGI Program to Run
Will Bontrager