次の方法で共有


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 #include directive to include it in other .asp files that call the procedures. Alternatively, you could encapsulate the functionality in a COM 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. 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> 
ms525319.alert_caution(en-us,VS.90).gifImportant Note:

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 code is executed in the following order: Script blocks in non-default languages are executed in order of appearance, then the inline code, and finally the script blocks in the default language. In addition, you cannot use the ASP output directive <%=%> within a procedure. Instead, use Response.Write to send content to the browser.

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 the JScript procedure from within VBScript--> 
  <% call printDate() %> 
  <!--Call the VBScript procedure from within VBScrip--> 
<% Echo %>  
  <BR> 
  </BODY> 
</HTML> 

<%Sub Echo%> 
<!--Note: this will not output anything unless the page is called with a query string like https://localhost/test.asp?x=1%20have&y=a%20cunning&z=plan --> 
<% 
  Response.Write "<TABLE BORDER=1>" & _ 
    "<TR><TH>Name</TH><TH>Value</TH></TR>"  

  Set objQueryString = Request.QueryString  

  For Each strSelection In objQueryString 
    Response.Write "<TR><TD>" & strSelection & "</TD><TD>" & _  
    objQueryString(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())  
  Response.Write("<BR>") 
}  
</SCRIPT> 

Note

VBScript calls to JScript functions are not case sensitive.

Passing Variables to Procedures

The scripting language you are using specifies how variables are passed to procedures. For example, JScript passes variables to a function by value only. VBScript can pass variables to a sub or function either by value or by reference.

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. For more information, see the Scripting SDK.