CGI: VBScript Banner Footer
VBScript and JavaScript CGI
VBScript Banner and Footer
Generally web pages consist of three parts: a banner, page content and a footer.
This page covers a very basic page template and a function to simulate PHP’s file include.
Test Script 4
Basic page templateA sbsic page template is shown on right.
Note: Replace HTML single parameters quotes with double quotes; for example, style="color:red" becomes style=""color:red""
|
'!c:/windows/system32/cscript //nologo Wscript.Echo "Content-type: text/html" & vbLF & vbLF WScript.Echo "<html>" WScript.Echo "<title>Test 4</title>" WScript.Echo "<body>" '--Banner Wscript.Echo "<h2 style=""color:red"" >Banner CGI Test 4</h2>" '--Content WScript.Echo "<p>VBScript CGI Test 4<br />" WScript.Echo "Page Content</p>" '--Footer Wscript.Echo "<h3 style=""color:green"">Footer CGI Test 4</h3>" WScript.Echo "</body>" WScript.Echo "</html>" Wscript.Quit 0 |
The script produces output as shown on right. Admittedly it's 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 4 VBScript CGI Test 4 Page Content Footer CGI Test 4 |
File Includes
Changing either the banner or footer requires editing each page in your site. This is time consuming and prone to errors. The ideal solution is to save the banner and footer code in external files and include these with appropriate include statements.
The following may come as a surprise: unlike PHP, there is no way in either VBScript or JavaScript to directly include an external file.
Simulate PHP File IncludesFunction shown on right simulates PHP’s file include by using the VBScript ExecuteGlobal function.
|
'============================================================================== ' This sub simulates PHP's include directive sub includeFile (fSpec) dim objFSO,objFile,fileData Set objFSO = CreateObject("Scripting.FileSystemObject")'* Create file obj Set objFile = objFSO.OpenTextFile(fSpec) '* Open file for read fileData = objFile.readAll() '* Read file to string objFile.close '* Close file executeGlobal fileData '* Run code in string set objFSO = nothing '* Clean-up remove obj set objFile = nothing '* Clean-up remove obj end sub '============================================================================== |
Test Script 5
In test folder UniServer\www\vbs_test, create three new files as shown below:
This file contains code that generates your site banner. |
test5_banner.vbs Wscript.Echo "<h2 style=""color:red"">Banner CGI Test 5</h2>" |
This file contains code that generates your site footer. |
test5_footer.vbs Wscript.Echo "<h3 style=""color:green"">Footer CGI Test 5</h3>" |
Test scriptThe test page includes the above function, and the original banner and footer text have been replaced with calls to this function. These calls pull in the appropriate files and run the code they contain. Run test script
ResultResult shown below: Banner CGI Test 5 VBScript CGI Test 5 Page Content Footer CGI Test 5 A slight disadvantage with this solution is that the simulated include file function must be included on every page of your site. That said, it is a relatively small amount of code, resulting in a positive advantage of having the ability to include files. Note: Included files are not restricted to the file extension .vbs . Any other suitable extension can be used, such as .inc or .txt . |
test5_footer.vbs '!c:/windows/system32/cscript //nologo Wscript.Echo "Content-type: text/html" & vbLF & vbLF WScript.Echo "<html>" WScript.Echo "<title>Test 5</title>" WScript.Echo "<body>" '--Banner includeFile("test5_banner.vbs") '--Content WScript.Echo "<p>VBScript CGI Test 5<br />" WScript.Echo "Page Content</p>" '--Footer includeFile("test5_footer.vbs") WScript.Echo "</body>" WScript.Echo "</html>" '============================================================================== ' This sub simulates PHP's include directive sub includeFile (fSpec) dim objFSO,objFile,fileData Set objFSO = CreateObject("Scripting.FileSystemObject")'* Create file obj Set objFile = objFSO.OpenTextFile(fSpec) '* Open file for read fileData = objFile.readAll() '* Read file to string objFile.close '* Close file executeGlobal fileData '* Run code in string set objFSO = nothing '* Clean-up remove obj set objFile = nothing '* Clean-up remove obj end sub '============================================================================== Wscript.Quit 0 |
Summary
The include file function is not limited to loading a banner or footer file. It also allows you to have a separate file containing a set of common functions for your site.
Although the above include file function can be used, there is a better alternative. This is covered in the JavaScript section.
Where to next
An import reason for using CGI scripts is to process data sent with a page request. For example, this can be data entered in a form. The data sent is in a special format, referred to as URL encoding. The next page covers URL Encoding.