c-- styles for logos and headline links do not modify internet, red, or black styles -->

Intranet Journal   Earthweb  
Events Jobs Premium Services Media Kit Network Map E-mail Offers Vendor Solutions Webcasts

   Intranet Journal Subjects
Search Earthweb

Privacy Policy



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

internet commerce
Be a Commerce Partner
















 

[ Home | Discussion Forum | How Do I... | Lotus Notes Intranets | Microsoft SharePoint | Products | Shopping  ]

free news!

VBScript in a Nutshell


 

VBScript in a Nutshell

By Paul Lomax, Matt Childs & Ron Petrusha


Chapter Excerpt: VBScript Program Structure

  • Introduction
  • Defining Subroutines: The Sub . . . End Sub Construct
  • Calling a Subroutine
  • Passing Variables into a Subroutine
  • Exiting a Routine with the Exit Statement
  • The Class Construct, Variables, Properties, Methods and Events
  • The Script Level: Active Server Pages
  • Windows Script Host
  • Client-Side Scripts for MSIE
  • Outlook Forms
  • Reusable Code Libraries: Active Server Pages
  • Reusable Code Libraries: Windows Script Host
  • Reusable Code Libraries: Client-Side Scripts for MSIE

    Printer Friendly Version

    The Script Level

    We've seen that code can be organized into functions, subroutines, and classes, and that some subroutines (and an occasional function) can be executed automatically if they are event handlers and the event they handle fires. However, that seems to offer a relatively limited "hook" for script to run, nor does it seem to make it possible for a script to perform whatever initialization might be required in order for its event handlers to function successfully.

    The script level is the answer to this dilemma. Script-level code--that is, code outside functions and subroutines--is executed automatically when the script loads or as the HTML on the page is parsed. The precise meaning of script-level code and the exact way in which code at script level is executed depends on the host environment for which the script is written. We'll examine these in turn.

    Active Server Pages

    In ASP, script-level code is synonymous with code in direct ASP commands--it is script that is preceded by the <% or <%= tags and terminated by the %> tag. (For details on how script is embedded within in ASP page, see Chapter 5, VBScript with Active Server Pages.) This code is executed automatically as the page's HTML is parsed.

    It is also possible to include script-level code in <SCRIPT>...</SCRIPT> tags in an ASP. However, this is not genuine script-level code: aside from variable declarations, the order in which this code is executed is undefined.

    Figure 2-1 shows the web page produced by Example 2-7, which illustrates script-level code in an Active Server Page. Note that although the variable x is defined and assigned a value at script level within the <SCRIPT> tag, the variable declaration is recognized but the variable assignment isn't. We can determine this because we've used the Option Explicit statement to require variable declaration, but the VBScript language engine did not raise an error when it first encountered the use of x on the second line after the <BODY> tag. But our assignment of 10 to x is not recognized, since the second line of our web page strongly suggests that x is uninitialized.

    Example 2-7: Script-Level Code in an Active Server Page
     <% Option Explicit %>
    <HEAD>
    <TITLE>Script-level code in ASP</TITLE>
    <SCRIPT LANGUAGE="VBScript" RUNAT="Server">
     
    Private x
    x = 10
     
    Private Function Increment(lVar)
       lVar = lVar + 1
       Increment = lVar
    End Function
     
    Private Function Decrement(lVar)
       lVar = lVar - 1
       Decrement = lVar
    End Function
     
    </SCRIPT>
    </HEAD>
    <BODY>
    <H2><CENTER>An Active Server Page</CENTER></H2>
    The current value of x is <%= x %> <BR>
    <%
       Dim y
       y = 20
       If x = 0 Then x = 10
    %>
    Value returned by Increment function: <%= Increment(x) %> <BR>
    Value returned by Increment function: <%= Increment(x) %> <BR>
    Value returned by Decrement function: <%= Decrement(x) %> <BR>
    The value of <I>x</I> is now <%= x %>.
    The value of <I>y</I> is <%= y %>.
    </BODY>
    </HTML>
    

    Figure 2-1.The web page produced by Example 2-7

     

    The conclusions to be drawn from Example 2-7 are unusually clear:

    • Variable declarations placed at script level within the <SCRIPT> tag are recognized by ASP.

    • Aside from variable declarations, no script-level code should be placed within the <SCRIPT> tag. The remaining code located within a <SCRIPT> tag should consist solely of function, subroutine, and class definitions.

    • Direct commands can contain any script-level code.

    • All direct commands are executed as the web server is parsing the HTML and generating a response to the client. In other words, along with handlers for the events supported by ASP (Application_OnStart, Application_OnEnd, Session_OnStart, Session_OnEnd, OnTransactionAbort, and OnTransactionCommit), direct commands are basic "hooks" that allow your code to run.

    Printer Friendly Version

  • Introduction
  • Defining Subroutines: The Sub . . . End Sub Construct
  • Calling a Subroutine
  • Passing Variables into a Subroutine
  • Exiting a Routine with the Exit Statement
  • The Class Construct, Variables, Properties, Methods and Events
  • The Script Level: Active Server Pages
  • Windows Script Host
  • Client-Side Scripts for MSIE
  • Outlook Forms
  • Reusable Code Libraries: Active Server Pages
  • Reusable Code Libraries: Windows Script Host
  • Reusable Code Libraries: Client-Side Scripts for MSIE

  • Of Interest
    · The Elements of Intranet Style
    · A Tutorial in VBScript