Auto-Launch CGI Script with JavaScript Tag
Should you need to run a CGI script when a web page loads, a JavaScript tag can be utilized. The SRC of the JavaScript tag is the URL of the script.
The CGI script runs, doing what it's programmed to do for you. Before the script quits, it sends some JavaScript code to the browser, satisfying the browser's request. The JavaScript can be an empty variable declaration or it can be code that actually does something for the web page.
This article has examples of everything you need to run a CGI program when a page loads by putting the URL of the program into a JavaScript tag.
Note: The introduction article, "Running a CGI Program On Page Load," contains numerous examples for automatically running a script when a page loads and has links to several different methods of implementing those features for your web site.
Launching a CGI Script Automatically with a JavaScript Tag
First, let's have a look at an example JavaScript tag:
<script type="text/javascript" language="JavaScript" src="/cgi-bin/script.cgi"> </script>
Assign the URL of your CGI script to the SRC attribute where
When the web page loads, the browser makes a request for JavaScript code to the URL in the SRC. The script at the URL does whatever it does and then returns some JavaScript code to the browser.
Here is an example CGI script that sends an empty variable declaration to the browser:
#!/usr/bin/perl use strict; # CGI program can do other stuff here. print "Content-type: text/javascript\n\n"; print "\n"; # end of program
It should be possible to copy the four "print..." lines in the example and paste them into the CGI program of your choice.
Put them where they won't interfere with the normal operation of the program. At a point just before the CGI program exits is probably the best place for it.
The CGI program used in this implementation may not return an HTML web page. It must return only JavaScript code to the browser. If it returned a web page, and the browser was expecting JavaScript, the browser will determine it has encountered JavaScript coding errors.
Will Bontrager