CGI: JavaScript Banner Footer

From The Uniform Server Wiki
Jump to navigation Jump to search

JavaScript Banner and Footer 1

This page covers including banner and footer files into a JavaScript CGI script.

Unlike VBScript a special function to include external files is not required.

This functionality is an inherent property of wsf files.


Include Files

Neither VBScript or JavaScript have the capability to directly include a file however the wsf container allows any number of files to be included. These included files must contain valid script commands. The format of an include command is shown below:

<script language="JScript" src="somefile.js"/>;

Note 1: Script tags <script .... /> cannot be nested. Generally these script tags are located at the top of a wsf script.

Note 2: There is no restriction on file extension you can use for example .txt or .inc the choice is yours.

Test Script 11 - Include files

In test folder \www\vbs_test create three new files as shown blow:

banner11.inc
This file contains JavaScript code.
The function generates your site banner.

//Banner function
function banner11()
{
  WSH.Echo("<h2 style=\"color:red\" >Banner CGI Test 11</h2>");
}

footer11.inc
This file contains JavaScript code.
The function generates your site footer.

//Footer function
function footer11()
{
  WSH.Echo("<h3 style=\"color:green\">Footer CGI Test 11</h3>");
}

Test script:

Two lines below the job tag inclues the above two functions. Each file includes the original banner and footer text enclosed in functions.

These functions banner11() and footer11() are called at the appropriate place in the web page script.

At first sight this looks like overkill why not include both banner and footer in a single file? You can the choice is yours.

The footer include file could contain the last two closing html tags since these are common to every web page.

The footer include file would look like:

//Footer function
function footer11()
{
  WSH.Echo("<h3 style=\"color:green\">Footer CGI Test 11</h3>");
  WSH.Echo("</body>");
  WSH.Echo("</html>");
}

test11.wsf

'!c:/windows/system32/cscript //nologo
<job>
<script language="JScript" src="banner11.inc"/>;
<script language="JScript" src="footer11.inc"/>;

<script language="JScript">
  WSH.Echo("Content-type: text/html \n\n");

  WSH.Echo("<html>");
  WSH.Echo("<title>Test 11</title>");
  WSH.Echo("<body>");

//--Banner
  banner11(); //Call banner function

//--Content
  WSH.Echo("<p>JavaScript CGI Test 11<br />");
  WSH.Echo("Page Content</p>");

//--Footer
  footer11(); //Call footer function

  WSH.Echo("</body>");
  WSH.Echo("</html>");
</script>

</job>

Run test script:

Banner CGI Test 11

JavaScript CGI Test 11

Page Content

Footer CGI Test 11

Top

Summary

WSF files allow you to easily include external files such as banner and footer files.

Any functions you regularly use can be contained in an external file and included as appropriate. For example next page covers the Get CGI variables function this is an ideal candidate.

Where to next

Next page covers a Get CGI variables function written in JavaScript (jScript).

Top