first_page the funky knowledge base
personal notes from way, _way_ back and maybe today

VBScript and ASP Design: Minimizing the Size of Script Blocks

I use the Variant vHTML in my VBScript for ASP. This variable appears at the top of the script and concatenates itself and new HTML along the way to end of the script passing decision structures (and even calls to external functions in Include (.INC) files). Any HTML to be rendered by ASP is stored in this variable. This is one (of many) ways to render HTML in a script block:

<%@ LANGUAGE="VBSCRIPT" %>
<%
    Option Explicit

    Dim vAgent, vHTML

    vAgent = Request.ServerVariables("HTTP_USER_AGENT")

    If Instr(1,vAgent,"Mozilla/4",vbTextCompare) > 0 _
        And _
        Instr(1,vAgent,"Mac",vbTextCompare) = 0 Then

        vHTML = "<H1>Hey!</H1>" _
            & "You are Mozilla 4 surfing" & Space(1) _
            & "but you are not on a Mac!"

    ElseIf Instr(1,vAgent,"Mozilla/4",vbTextCompare) > 0 _
        And _
        Instr(1,vAgent,"Mac",vbTextCompare) > 0 Then

        vHTML = "<H1>Hey!</H1>" _
            & "You are Mozilla 4 surfing" & Space(1) _
            & "and you <EM>are</EM> on a Mac!"

    Else

        vHTML = "<H1>Hey!</H1>" _
            & "What kind of browser is this?"
    End If

    vHTML = "<HTML>" & vbCrLf _
        & "<BODY>" & vbCrlf _
        & vHTML
        & "</BODY>" & vbCrlf _
        & "</HTML>"

    Response.Write vHTML
%>

In the above example, it appears that I am using two script blocks instead of one. This is true. But note the '@' sign in the first block. This means, "This script block contains Directives." And Directives (e.g. "LANGUAGE") must be separate from the script code which in this case is VBScript ---following the LANGUAGE Directive of course. Script blocks containing directives must start at line one (1) of the .ASP page.

To make things worse, I would need three separate script blocks when the #include server-side directive is used. This would be the form:

<%@ LANGUAGE="VBSCRIPT" %>
<% Option Explicit %>
<!--#include file="adovbs.inc"-->
<%
    'This is the main script block.
%>

where the file adovbs.inc is the ADO constants file provided by Microsoft. Note that each file referenced by the #include directive may contain additional script blocks. The reason why the Option Explicit statement needs its own script block is that it must be the first line in the VBScript code. The assumption here is that the files referenced by #include contain VBScript blocks. The #include directive is the only server-side directive supported by .ASP pages (ASP.DLL); the others, by the way, are supported by SSINC.DLL (.SHTML or .STM pages by default).

The main reasons to minimize script blocks is to increase runtime compilation speed and to reduce CPU usage. For more information please see "Compiling Large ASP Pages Can Take 100% of CPU Time" (Q193831) in the Microsoft Knowledge Base.

mod date: 1999-12-13T06:17:14.000Z