Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Automation

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

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 src="/cgi-bin/script.cgi" is assigned in the example.

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

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC