Home

ASP ADVANCED

An Active Server Page is primarily a scripting environment.  You can use other scripting languages with Active Server Pages as well.  Any scripting language that has a scripting engine compatible with the ActiveX scripting standard can be used in an Active Server Page.

The easiest way to add a script to an Active Server Page is by using the script delimiters <% and %>. Any text enclosed within these delimiters will be processed as a script.  Here's an example:

<HTML>  
<HEAD><TITLE>ASP script</TITLE></HEAD>  
<BODY>  
This is a  
<% FOR i=1 TO 10 %>  
very,  
<% NEXT %>  
very long sentence.  
<BODY>  
</HTML>

When this Active Server Page is displayed by a Web browser, the following sentence is displayed:

This is a very, very, very, very, very, very, very, very, very, very, very long sentence.

The script creates 11 copies of the word very by using a VBScript FOR ... NEXT loop.

By default, an Active Server Page assumes you'll be using VBScript as your primary scripting language.  This means that you don't need to do anything beyond using the <% and %> script delimiters to use this language.  However, there are three ways to explicitly specify a language to use in an Active Server Page.

First, you can use the Internet Service Manager to specify a particular scripting language as the default language for all your Active Server Pages.  To do this, perform the following steps:

  1. Launch the Internet Service Manager from the Microsoft Internet Information Server program group on the Start menu.

  2. Right-click the name of your Web site.  If you haven't changed the default configuration, the name of this Web site will be Default Web Site.

  3. Choose Properties.

  4. Click the Home Directory tab.

  5. Click the Configuration button. (To do this, you must have an existing application.  If you don't, create one now by clicking Create.)

  6. In the Application Configuration dialog box, click the App Options tab.

  7. In the Default ASP Language text box, enter the name of the scripting language that you want to use as your primary scripting language; for example, enter vbscript or jscript.

After you have specified a particular scripting language as the default scripting language, you can use it in your Active Server Pages simply by using the delimiters <% and %>.  If you plan to use jscript for the majority of your Active Server Pages, for example, you should configure this language as your default language.

You can also specify the primary scripting language for a particular page. To do this, place the LANGUAGE directive as the very first line in your Active Server Page file, like this:

<%@ LANGUAGE=JScript %>
<HTML>
<HEAD><TITLE>ASP script</TITLE></HEAD>  
<BODY>  
This is a  
<% for (i=1;i<11;i++){ %>  
very,  
<% } %>
very long sentence.  
</BODY>  
</HTML>  

The directive in the first line of this script indicates that all the scripts contained in the file should be executed as scripts created with JScript rather than some other scripting language.  When you use this directive, be sure to include a space between the @ character and the key­word LANGUAGE.  Furthermore, it's very important that this directive appear before any other commands in your Active Server Page file (otherwise you'll get an error).

Yet a third alternative exists for including scripts in your Active Server Pages.  You can use Microsoft's extended <SCRIPT> HTML tag, as in this example: 

<HTML>
<HEAD><TITLE>ASP script</TITLE></HEAD>
<BODY>
<SCRIPT LANGUAGE="JScript" RUNAT="server">
function sayhello( )
{
response.write('Hello!")
}
</SCRIPT>
<%
sayhello( ) 
%> 
</BODY>
</HTML>

Here, the <SCRIPT> tags contain a JScript function.  The LANGUAGE attribute of the <SCRIPT> tag specifies which scripting language to use.  The RUNAT attribute indicates that the script should be executed on the Server rather than the Client (the browser). The function named Sayhello( )  is defined in the first script.  The second script, marked with the usual <% and %> tags, is where the JScript function is actually called.  This Active Server Page prints the text Hello! to the screen.

If you have created client-side JScript or VBScript scripts, you should be familiar with the <SCRIPT> tag.  Microsoft's extended <SCRIPT> tag can be used to specify either client-side or server­side scripts.  If you neglected to include the RUNAT="server"
attribute in the preceding example, the script would be treated as a client-side script.  In this case, the server would ignore the script and the browser would attempt to execute the script (and it would fail miserably, because the script isn't a valid client-side script).

Why would you ever want to use the <SCRIPT> tag rather than the <% and %> script delimiters?  Normally, you wouldn't use the <SCRIPT> tag.  However, there are two significant differences between these two ways of specifying a script.

First, scripts that are contained in the <SCRIPT> tag are executed immediately, no matter where they appear in an Active Server Page.  For example, consider the following page:

<HTML>
<HEAD><TITLE>ASP Script</TITLE></HEAD>
<BODY>
This is the first sentence.
<SCRIPT LANGUAGE="JScript" RUNAT="Server">
response.write("This is the second sentence.")
</SCRIPT>
</BODY>
</HTML>

From looking at the script, you might be tempted to believe that the sentence "This is the first sentence." and the sentence "This is the second sentence." are printed to the screen in that order.  However, when the Active Server Page is displayed in a browser, the order of the two sentences is actually reversed (or worse, nothing is displayed, because an invalid HTML page is generated).

Why does this happen?  Whatever is contained in the <SCRIPT> tag is executed before anything else in a page.  If you use the VIEW SOURCE command on your Web browser, you'll see the following results from the preceding Active Server Page:

This is the second sentence.<HTML>
<HEAD><TITLE>ASP Script</TITLE></HEAD>
<BODY>
This is the first sentence.
</BODY>
</HTML>

This behavior of the <SCRIPT> tag has two implications.  First, you can place scripts contained in a <SCRIPT> tag anywhere you want in an Active Server Page.  Second, the <SCRIPT> tag, for most purposes, is restricted to containing functions or procedures.  The output of any script that's not contained in a function or procedure is displayed immediately and results in an invalid HTML page.

The <SCRIPT> tag has one main advantage over the <% and%> script delimiters.  Using the <SCRIPT> tag, you can mix multiple scripting languages within a single Active Server Page.  Consider the following example:  

<%@ LANGUAGE="VBScript" %>
<HTML>
<HEAD><TITLE>ASP Script</TITLE></HEAD>
<BODY>
<SCRIPT LANGUAGE="JScript" RUNAT="server">
function sayhello( )
{
response.write("Hello!")
}
</SCRIPT>
<%
FOR i=1 to 10
sayhello( )
NEXT
</BODY>
</HTML> 

This script prints Hello! 10 times in a row.  But notice how it does this.  The script contained in the <% and %> delimiters is a Visual Basic script.  However, this script calls JScript function.  The JScript function is defined within the <SCRIPT> tag. When you want to use one scripting language as your primary scripting language, but need to call a function from another language, you can use this method.  This is useful when one language has particular functions or methods that another language lacks.

To summarize, there are three methods of including a script in an Active Server Page:

  • Specify a scripting language for all of your Active Server Pages by using the Internet Service Manager to specify a default language.

  • Specify a scripting language for a single page by using the Active Server Page directive <%@ LANGUAGE="scripting language" %>.

  • Mix multiple scripting languages in a single Active Server Page by using the extended <SCRIPT> tag.

Before ending this section, one final Active Server Pages directive should be discussed.  By using the Active Server Pages output directive, you can display the value of an expression.  Here's an example:

<HTML>
<HEAD><TITLE>ASP Example</TITLE></HEAD>
<BODY>
At the tone, the time will be: <%=TIME%>
</BODY>
</HTML>

You use the delimiters <%= and %> to print the value of a variable, method, or function.  In the preceding example, the output directive is used to output the value of the VBScript TIME function.

There's another way to accomplish the same thing.  Consider the following example:

<HTML>
<HEAD><TITLE>ASP Example</TITLE></HEAD>
<BODY>
At the tone, the time will be: <% Response.write(TIME) %>
</BODY>
</HTML>

In this example, the value of the VBScript TIME function is outputted by using the Active Serer Pages Response object.  The Write( ) method of the Response object outputs the value of expressions to the screen. (In the next section, you learn more about using objects.)

When should you use the Response.Write( ) method rather than the <%= and %> output directive?  It really doesn't matter.  Active Server Pages internally represents the output directive as a Response.Write ( ) method call in any case.  The two methods of outputting the values of expressions are completely interchangeable.

Nevertheless, there are situations where one way of outputting the values of expressions is more convenient than the other.  For example, when you need to output the value of an expression within a script, the Response.Write( ) method is often easier to use.  On the other hand, when you want to output the value of an expression within a section of HTML code, the <%= and %> directive is often easier to use.  The following page illustrates both approaches:

<HTML>
<HEAD><TITLE>ASP Example</TITLE></HEAD>
<BODY>
<%

FOR i=1 TO 10
myvar=myvar&"very,"
Response.Write(i&":"&myvar&"<BR>")
NEXT
%>
<HR>
This is a <%=myvar%> long sentence.
</BODY>
</HTML>

In this example, the Response.Write( ) method is used within the loop to display the value of the variable named myvar as it increases in size.  The <%= and %> output directive is embedded within normal HTML code.  The output directive is used to display the value of myvar (see below figure)

Two methods of outputting expressions

Two methods of outputting expressions