https://wiki.uniformserver.com/index.php?title=CGI:_JavaScript_CGI&feed=atom&action=historyCGI: JavaScript CGI - Revision history2024-03-28T15:57:21ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.uniformserver.com/index.php?title=CGI:_JavaScript_CGI&diff=6073&oldid=prevRic: 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..."2011-11-07T23:18:17Z<p>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..."</p>
<p><b>New page</b></p><div>{{Nav CGI}}<br />
'''JavaScript CGI'''<br />
<br />
This page covers running JavaScripts (JScrpit) as CGI scripts.<br />
<br />
It assumes the Apache server has been configured to permit running of CGI scripts<br />
<br />
and test folder '''vbs_test''' and file '''.htaccess''' have been created as explained on the [[CGI: Introduction| introduction page]].<br />
<br />
__TOC__<br />
<br />
==Shebang Problem==<br />
You cannot directly run JavaScript scripts as CGI on the Apache server!<br /><br />
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.<br />
<br />
A JavaScript comment consists of two forward slash characters '''//''' hence its impossible to create a valid shebang line.<br />
<br />
The following are examples of valid Perl and VBScript shebang lines:<br />
<br />
* '''#!'''C:/UniServer/usr/bin/perl.exe<br />
* ''''!'''C:/windows/system32/cscript –nologo<br />
<br />
'''Note''' the two-character shebangs are '''#!''' and ''''!''' <br />
<br />
===Solution - Use wsf files===<br />
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:<br />
<br />
* ''''!C:/windows/system32/cscript –nologo''' - Note it is identical to a VBScript shebang.<br />
<br />
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.<br />
<br />
'''Main features of wsf files:'''<br />
<br />
* '''Include statements''' Incorporate functions from VBScript or JScript files into your Windows Script Host project.<br />
* '''Multiple engines''' Use more than one scripting language per file.<br />
* '''Type libraries''' Add constants to your code.<br />
* '''Tools''' Edit files with any XML editor.<br />
* '''Multiple jobs in one file''' Store all of your code in a single location.<br />
<br />
{|<br />
|-valign="top"<br />
|<br />
===Basic swf file format===<br />
* Comments start with a single quote.<br />
* WSF files must contain at least one job tag.<br />
* Inside this job tag you can have more than one script tag.<br />Each script tag must define a language to use.<br />
* A script tag can include an external file using '''src="file name'''"<br />Note: Code inside an included file is immediately executed.<br />
* Each script tag pair '''<script .... ></script>''' contains code<br />appropriate to scripting language defined.<br />
<br />
|<br />
<pre><br />
'Comment<br />
<job><br />
<br />
<script language="JScript" src="some_file.js"/><br />
<script language="VBScript" src="some_file.vbs"/><br />
<br />
<script language="JScript"><br />
WSH.Echo("Hello world");<br />
</script><br />
<br />
<script language="VBScript"><br />
WScript.Echo "Hello world"<br />
</script><br />
<br />
</job><br />
<br />
</pre><br />
|}<br />
<br />
'''''[[#top | Top]]'''''<br />
==Test Script 9 - Basic JavaScript (JScript) CGI==<br />
To run wsf files as CGI scripts Apache needs to be configured to permit files with that extension.<br />
{|<br />
|-<br />
|<br />
Edit file \www\vbs_test\'''.htaccess'''<br />
<br />
Add the extension '''.wsf''' to the AddHandler line as shown on right.<br />
|<br />
<pre><br />
AddHandler cgi-script .vbs .wsf <br />
Options +ExecCGI<br />
</pre><br />
|}<br />
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.<br />
<br />
{|<br />
|-<br />
|<br />
===Basic JavaScript CGI template===<br />
* Create a new file '''test9.wsf''' with content as shown on right.<br />
* Start Apache if not already running<br />
* Enter: <nowiki>http://localhost:8081/vbs_test/test9.wsf</nowiki><br />
|<br />
<pre><br />
'!c:/windows/system32/cscript //nologo<br />
<job><br />
<script language="JScript"><br />
WSH.Echo("Content-type: text/html \n\n");<br />
WSH.Echo("Hello world");<br />
</script><br />
</job><br />
</pre><br />
|}<br />
'''Hello world''' displayed in browser confirms correct operation.<br />
<br />
'''''[[#top | Top]]'''''<br />
==Test Script 10 - Basic JavaScript CGI page template==<br />
{|<br />
|-valign="top"<br />
|<br />
'''Basic page template:'''<br />
<br />
Basic page template is shown on right.<br />
<br />
* Every page starts with a shebang line.<br />
* This is followed the opening job tag <job>.<br />
* First line output by the script must be the Content-type header.<br />
* 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.<br />
* The page body is split into three sections banner, content and footer<br />
* Next two lines are html closing tags.<br />
* Last two lines are script and job closing tags respectively.<br />
<br />
'''Note''': Precede HTML single parameter quotes with a backslash for example style="color:red" becomes style=\"color:red\"<br />
<br />
<br />
'''Test script''':<br />
* Create a new file '''test10.wsf''' with content as shown on right.<br />
* Start Apache if not already running<br />
* Enter: <nowiki>http://localhost:8081/vbs_test/test10.wsf</nowiki><br />
|<br />
<pre><br />
'!c:/windows/system32/cscript //nologo<br />
<job><br />
<script language="JScript"><br />
WSH.Echo("Content-type: text/html \n\n");<br />
<br />
WSH.Echo("<html>");<br />
WSH.Echo("<title>Test 10</title>");<br />
WSH.Echo("<body>");<br />
<br />
//--Banner<br />
WSH.Echo("<h2 style=\"color:red\" >Banner CGI Test 10</h2>");<br />
<br />
//--Content<br />
WSH.Echo("<p>JavaScript CGI Test 10<br />");<br />
WSH.Echo("Page Content</p>");<br />
<br />
//--Footer<br />
WSH.Echo("<h3 style=\"color:green\">Footer CGI Test 10</h3>");<br />
<br />
WSH.Echo("</body>");<br />
WSH.Echo("</html>");<br />
<br />
</script><br />
</job><br />
<br />
</pre><br />
<br />
|-<br />
|<br />
The script produces output as shown on right.<br /><br />
Admittedly nothing spectacular a real page banner may include<br /><br />
such items as images, text or even news announcements.<br /><br />
Similarly the footer can be as complex as you like.<br /><br />
<br />
Generally the same banner and footer are used on every page<br /><br />
providing your site with a consistent look.<br />
<br />
|style="background:#f5f5f5;"| <br />
<br />
<span style="font-weight:bold;font-size:16px;color:red">Banner CGI Test 10</span><br />
<br />
JavaScript CGI Test 10<br />
<br />
Page Content<br />
<br />
<span style="font-weight:bold;font-size:16px;color:green">Footer CGI Test 10</span><br />
<br />
|}<br />
<br />
<br />
'''''[[#top | Top]]'''''<br />
== Summary ==<br />
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.<br />
<br />
==Where to next==<br />
[[CGI: JavaScript Banner Footer|Next page]] covers adding a banner and footer using wsf include files.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
----<br />
<br />
[[Category: Tutorials]]<br />
[[Category: How To]]<br />
[[Category: Mini Servers]]</div>Ric