CGI: JavaScript CGI

Revision as of 23:18, 7 November 2011 by Ric (talk | contribs) (Created page with "{{Nav 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 script...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

  • Comments start with a single quote.
  • WSF files must contain at least one job tag.
  • Inside this job tag you can have more than one script tag.
    Each script tag must define a language to use.
  • A script tag can include an external file using src="file name"
    Note: Code inside an included file is immediately executed.
  • Each script tag pair <script .... ></script> contains code
    appropriate to scripting language defined.
'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>

Top

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

  • Create a new file test9.wsf with content as shown on right.
  • Start Apache if not already running
  • Enter: http://localhost:8081/vbs_test/test9.wsf
'!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.

Top

Test Script 10 - Basic JavaScript CGI page template

Basic page template:

Basic page template is shown on right.

  • Every page starts with a shebang line.
  • This is followed the opening job tag <job>.
  • First line output by the script must be the Content-type header.
  • Next three lines are the minimum tags required for an html page. You can add tags such as meta, link and script as required for your application.
  • The page body is split into three sections banner, content and footer
  • Next two lines are html closing tags.
  • Last two lines are script and job closing tags respectively.

Note: Precede HTML single parameter quotes with a backslash for example style="color:red" becomes style=\"color:red\"


Test script:

  • Create a new file test10.wsf with content as shown on right.
  • Start Apache if not already running
  • Enter: http://localhost:8081/vbs_test/test10.wsf
'!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.
Admittedly nothing spectacular a real page banner may include
such items as images, text or even news announcements.
Similarly the footer can be as complex as you like.

Generally the same banner and footer are used on every page
providing your site with a consistent look.

Banner CGI Test 10

JavaScript CGI Test 10

Page Content

Footer CGI Test 10


Top

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.

Top