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 #include directive to include it in other .asp files that call the procedures. Alternatively, you could encapsulate the functionality in a COM component.
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. However, 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 LANGUAGE=JScript RUNAT=SERVER> 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 installed with the server. 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 LANGUAGE=JScript RUNAT=SERVER > function MyFunction() { Response.Write("You called MyFunction().") } </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 these commands may be executed in an uncertain order. In addition, you cannot use the ASP output directive <%= %>
within a procedure. Instead, use Response.Write to send content to the browser.
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 functions 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> <% Echo %> <BR> <% printDate() %> </BODY> </HTML> <% Sub Echo Response.Write "<TABLE>" & _ "<TR><TH>Name</TH><TH>Value</TH></TR>" Set objQueryString = Request.QueryString For Each strSelection In objQueryString Response.Write "<TR><TD>" & p & "</TD><TD>" & _ FormValues(strSelection) & "</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>
Note VBScript calls to JScript functions are not case sensitive.
To pass an entire array to a procedure in VBScript, use the array name followed by empty parentheses; in JScript, use empty square brackets.