ASP Functions

Code reuse is as important in Active Server Pages as it is in any other form of application programming. The first example of code reuse is the ASP function or subroutine. As I mentioned in the beginning of this chapter, there is one other way to delineate server-side code: the RUNAT attribute of the <SCRIPT> tag. You can use the RUNAT attribute to specify that a particular function or subroutine is to be run (and called from) the server side. Example 2.5 demonstrates the use of the RUNAT attribute to create a simple function that uses the last three letters of the domain string to return the general type of site that it represents. This function takes a domain string such as perl.ora.com and returns the string "company." The RUNAT attribute instructs ASP that this is a server-side-only function. It will not be sent to the client and is a valid function to call from within the server-side code. We could now incorporate that into a script, as shown in Example 2.6.

Example 2.5. Using the RUNAT Attribute to Create a Server-Side Function

<SCRIPT LANGUAGE = "VBScript" RUNAT = SERVER>
Function DomainType(strDomainString)

   strPossibleDomain = Right(strDomainString, 3)

   Select Case Ucase(strPossibleDomain)
      Case "COM"
         DomainType = "company"
      Case "EDU"
         DomainType = "educational"
      Case "GOV"
         DomainType = "government_civil"
      Case "MIL"
         DomainType = "government_military"
      Case Else
         DomainType = "UNKNOWN"
   End Select

End Function
</SCRIPT>

Example 2.6. Including a Server-Side Function ...

Get ASP in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.