Writing Procedures

A procedure is a group of script commands that performs a specific task and can return a value. You can define your own procedures and call them repeatedly in your scripts.

You can place procedure definitions in the same .asp file that calls the procedures, or you can put commonly used procedures in a shared .asp file and use the SSI #include directive to include it in other .asp files that call the procedures. Alternatively, you could package the functionality in an ActiveX component.

Defining Procedures

Procedure definitions can appear within <SCRIPT> and </SCRIPT> tags and must follow the rules for the declared scripting language. Use the <SCRIPT> element for procedures in languages other than the primary scripting language. Use the scripting delimiters (<% and %>) for procedures in the primary scripting language.

When you use the HTML <SCRIPT> tag, you must use two attributes to ensure server-side processing of the script. The syntax for using the <SCRIPT> tag is:

<SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
  procedure definition
</SCRIPT>

The RUNAT=SERVER attribute tells the Web server to process the script on the server. If you do not set this attribute, the script is processed by the client browser. The LANGUAGE attribute determines the scripting language used for this script block. You can specify any language for which you have the scripting engine. To specify VBScript, use the value VBSCRIPT. To specify JScript, use the value JSCRIPT. If you do not set the LANGUAGE attribute, the script block is interpreted in the primary scripting language.

The commands in the script block must form one or more complete procedures in the chosen scripting language. For example, the following commands define the JScript procedure MyFunction.

<HTML>
<SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
  function MyFunction()
  {
      Response.Write("MyFunction Called")
  }  
</SCRIPT>

Important    Do not include within server-side <SCRIPT> tags any script commands that are not part of complete procedures. Commands that are not part of a procedure may cause unpredictable results because the order of execution is not predictable. In addition, you cannot use the ASP output expression <%= within a procedure. Instead, you must use Response.Write.

Calling Procedures

To call procedures, include the name of the procedure in a command. If you are calling JScript procedures from VBScript, you must use parentheses after the procedure name; if the procedure has no arguments, use empty parentheses. If you are calling either VBScript or JScript procedures from JScript, always use parentheses after the procedure name.

For VBScript, you can also use the Call keyword when calling a procedure. However, if the procedure that you are calling requires arguments, the argument list must be enclosed in parentheses. If you omit the Call keyword, you also must omit the parentheses around the argument list. If you use Call syntax to call any built-in or user-defined function, the function’s return value is discarded.

The following example illustrates creating and calling procedures by using two different scripting languages (VBScript and JScript).

<%@ LANGUAGE = VBSCRIPT >%
<HTML> 
<BODY>
<% Call Echo %> 
<BR>
<% Call PrintDate() %>
</BODY>
</HTML>

<%
Sub Echo 
  Response.Write "<TABLE>" 
  Response.Write "<TR><TH>Name</TH><TH>Value</TH></TR>" 
  Set Params = Request.QueryString 
  For Each p in Params 
    Response.Write "<TR><TD>" & p & "</TD><TD>" & _ 
    Params(p) & "</TD></TR>" 
  Next 
Response.Write "</TABLE>" 
End Sub 
%>

<SCRIPT LANGUAGE=JScript RUNAT=Server> 
function PrintDate() 
{ 
  var x
  x = new Date() 
  Response.Write(x.toString()) 
} 
</SCRIPT>

Passing Arrays to Procedures

To pass an entire array to a procedure in VBScript, use the array name followed by empty parentheses; in JScript, use empty square brackets.