CGI: JavaScript CGI
VBScript and JavaScript CGI
JavaScript CGI
This page covers running JavaScripts (JScrpit) as CGI scripts.
It assumes the Apache server has been configured to permit running of CGI scripts
and test folder vbs_test and file .htaccess have been created as explained on the introduction page.
Shebang Problem
You cannot directly run JavaScript scripts as CGI on the Apache server!
JavaScript fails to meet the shebang line specification. A shebang consists of two characters, first character must be a comment followed by an exclamation mark. Immediately following the shebang is the path to an executable that will run the script. Following this is an optional list of parameters separated by spaces.
A JavaScript comment consists of two forward slash characters // hence its impossible to create a valid shebang line.
The following are examples of valid Perl and VBScript shebang lines:
- #!C:/UniServer/usr/bin/perl.exe
- '!C:/windows/system32/cscript –nologo
Note the two-character shebangs are #! and '!
Solution - Use wsf files
Solution to this problem is to use Windows script files (*.wsf) these are not engine-specific and act as a container. For Apache CGI scripts they have one import feature the first line of a script can use a two-character shebang. The shebang looks like this:
- '!C:/windows/system32/cscript –nologo - Note it is identical to a VBScript shebang.
In addition to this, wsf files can contain and run any scripts that are Windows Script compatible. Meaning you can use either VBScript or JavaScript or both in the same file.
Main features of wsf files:
- Include statements Incorporate functions from VBScript or JScript files into your Windows Script Host project.
- Multiple engines Use more than one scripting language per file.
- Type libraries Add constants to your code.
- Tools Edit files with any XML editor.
- Multiple jobs in one file Store all of your code in a single location.
Basic swf file format
|
'Comment <job> <script language="JScript" src="some_file.js"/> <script language="VBScript" src="some_file.vbs"/> <script language="JScript"> WSH.Echo("Hello world"); </script> <script language="VBScript"> WScript.Echo "Hello world" </script> </job> |
Test Script 9 - Basic JavaScript (JScript) CGI
To run wsf files as CGI scripts Apache needs to be configured to permit files with that extension.
Edit file \www\vbs_test\.htaccess Add the extension .wsf to the AddHandler line as shown on right. |
AddHandler cgi-script .vbs .wsf Options +ExecCGI |
Every script must start with a shebang, wsf files use the VBScript shebang. This shebang informs Apache to use the command line interpreter and to suppress any logo text that is output by default. The first line any CGI script outputs must be a valid Content-type header of particular importance are the two new line characters these are mandatory.
Basic JavaScript CGI template
|
'!c:/windows/system32/cscript //nologo <job> <script language="JScript"> WSH.Echo("Content-type: text/html \n\n"); WSH.Echo("Hello world"); </script> </job> |
Hello world displayed in browser confirms correct operation.
Test Script 10 - Basic JavaScript CGI page template
Basic page template: Basic page template is shown on right.
Note: Precede HTML single parameter quotes with a backslash for example style="color:red" becomes style=\"color:red\"
|
'!c:/windows/system32/cscript //nologo <job> <script language="JScript"> WSH.Echo("Content-type: text/html \n\n"); WSH.Echo("<html>"); WSH.Echo("<title>Test 10</title>"); WSH.Echo("<body>"); //--Banner WSH.Echo("<h2 style=\"color:red\" >Banner CGI Test 10</h2>"); //--Content WSH.Echo("<p>JavaScript CGI Test 10<br />"); WSH.Echo("Page Content</p>"); //--Footer WSH.Echo("<h3 style=\"color:green\">Footer CGI Test 10</h3>"); WSH.Echo("</body>"); WSH.Echo("</html>"); </script> </job> |
The script produces output as shown on right. Generally the same banner and footer are used on every page |
Banner CGI Test 10 JavaScript CGI Test 10 Page Content Footer CGI Test 10 |
Summary
Although using wsf files incur an overhead of four lines this slight disadvantage is outweighed by additional functionality offered. It would be impossible to run JavaScript CGI scripts on Apache without using wsf files.
Where to next
Next page covers adding a banner and footer using wsf include files.