|
|||||||
|
|
|
|||||
|
|
|||||||
Scripting languages are an intermediate stage between HTML and programming languages such as Java, C++, and Visual Basic. HTML is generally used for formatting and linking text. Programming languages are generally used for giving a series of complex instructions to computers. Scripting languages fall somewhere in between, although scripting languages function more like programming languages than simple HTML docs. The primary difference between scripting languages and programming languages is that the syntax and rules of scripting languages are less rigid and intricate than those of programming languages.
Scripting engines are the COM (Component Object Model) objects that process scripts. Active Server Pages provides a host environment for scripting engines and distributes scripts within .asp files to these engines for processing. For each scripting language that is used in coordination with ASP scripting, the related scripting engine must be installed on the Web server. For example, VBScript is the default language of Active Server Pages, so the VBScript engine resides as an COM object accessible by Active Server Pages so that it can process VBScript scripts. Likewise, Active Server Pages can provide a scripting environment for a number of other scripting languages, including JScript, REXX, and Perl, and others.
Active Server Pages makes it possible for the Web developer to write complete procedures by using a variety of scripting languages without having to worry about whether a browser supports them all. In fact, several scripting languages can be used within a single .asp file. This can be done by identifying the script language in an HTML tag at the beginning of the script procedure.
In addition, because scripts are read and processed on the server side, the client browser that requests the .asp file does not need to support scripting.
VBScript is the default scripting language that is used for primary scripting. If you use primary scripting, which uses the <% and %> delimiters, you can place any valid VBScript command inside the scripting delimiters and Active Server Pages will process the commands inside of the delimiters as VBScript. Active Server Pages makes it possible to set any scripting language as the primary scripting language. You can set the primary scripting language on a page-by-page basis, or for all pages on your Web server.
To change the primary scripting language for all pages in all applications, you must change the value of the DefaultScriptLanguage entry in the registry to that language. For more information about this registry entry, see Configuring Registry Entries.
The procedure for setting the primary scripting language depends on whether the chosen language supports Object.Method syntax. The procedures are detailed below.
For languages that support Object.Method syntax and use parentheses to enclose parameters, such as VBScript and JScript, you can change the primary scripting language for a single page by adding a command line to the beginning of your .asp file. The syntax for this command is:
<%@ LANGUAGE = ScriptingLanguage %>
where ScriptingLanguage is the
primary scripting language that you want to set for that particular page.
Follow these guidelines when setting the primary scripting language for a page:
@ and LANGUAGE. @ and LANGUAGE = ScriptingLanguage between the scripting
delimiters (<% and %>).
If you do not follow these guidelines for setting the primary scripting language for a page, an error will be generated.
In order to use a language that does not support the Object.Method syntax as the primary scripting language, you must first create the LanguageEngines registry key with the corresponding language name subkey and values:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services
\W3SVC
\ASP
\LanguageEngines
\LanguageName
Value: Write REG_SZ: Response.WriteEquiv |
Value: WriteBlock REG_SZ: Response.WriteBlockEquiv |
where LanguageName is the name of the
chosen language, Response.WriteEquiv is the
languages
equivalent of Response.Write, and Response.WriteBlockEquiv
is the languages
equivalent of Response.WriteBlock. The pipe symbol (|) is an insertion
used by Active Server Pages to send expressions and HTML blocks that are normally
processed with Response.Write and Response.WriteBlock
methods. This may be done automatically when installing additional scripting languages.
Note Some scripting languages are sensitive to white space or newline characters; it may not be possible to use such languages as the primary scripting language by changing the registry entries as described above. An alternative to using these as primary scripting languages is to manually write HTML blocks to the browser rather than using Active Server Pages to automatically handle interleaved <% ... %> script directives and HTML. Another option is to write that languages functions within tagged script blocks (<SCRIPT> ... </SCRIPT> ) and call them from any other language.
An attractive feature of Active Server Pages is the capability to incorporate several scripting language procedures within a single .asp file. With this functionality, you can use scripting languages that have particular strengths to help you get a specific job done.
A procedure is a group of script commands that performs a specific task. You can define your own procedures and call them repeatedly in your scripts. Procedure definitions can appear within <SCRIPT> and </SCRIPT> tags and must follow the rules for the declared scripting language. You can also define a procedure within scripting delimiters (<% and %>) as long as it is in the same scripting language as the primary script.
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 a server-side include statement (that is, <!--#INCLUDE FILE= ...) to include it in other .asp files that call the procedures. Alternatively, you could package the functionality in an ActiveX server component.
To call procedures, include the name of the procedure in a command.
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 functions return value is discarded. 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.
The following example illustrates creating and calling procedures by using two different scripting languages (VBScript and JScript).
<HTML>
<BODY>
<TABLE>
<% Call Echo %>
</TABLE>
<% Call PrintDate %>
</BODY>
</HTML>
<SCRIPT LANGUAGE=VBScript RUNAT=Server>
Sub Echo
Response.Write _
"<TR><TD>Name</TD><TD>Value</TD></TR>"
Set Params = Request.QueryString
For Each p in Params
Response.Write "<TR><TD>" & p & "</TD><TD>" & _
Params(p) & "</TD></TR>"
Next
End Sub
</SCRIPT>
<SCRIPT LANGUAGE=JScript RUNAT=Server>
function PrintDate()
{
var x
x = new Date()
Response.Write(x.toString())
}
</SCRIPT>
Note To pass an entire array to a procedure in VBScript, use the array name followed by empty parentheses; in JScript, use empty square brackets.
When using VBScript on the server with ASP, two VBScript features are disabled. Because Active Server Pages scripts are executed on the server, the VBScript statements that present user-interface elements, InputBox and MsgBox, are not supported. The VBScript functions CreateObject and GetObject are also not supported. Use of these statements will cause an error.
For a list and description of all VBScript operators, functions, statements, objects, properties, and methods, refer to the VBScript Language Reference.
For a list and description of all JScript operators, functions, statements, objects, properties, and methods, refer to the JScript Language Reference.
Because the processing of all ASP scripts is done on the server side, there is no need to include HTML comment tags to hide the scripts from browsers that do not support scripting, as is often done with client-side scripts. All ASP commands are processed before content is sent to the browser.
Basic REM and apostrophe-style comments are
supported in VBScript. Unlike HTML comments, these are removed when the script is
processed and are not sent to the client.
<% REM This line and the following two are comments 'The PrintTable function prints all 'the elements in an array. Call PrintTable(myarray()) %>
Important
You cannot include a comment in an output expression. For example, the first line that follows will work, but the second line will not, because it begins with<%=.
<% i = i +1 'this increments i. This script will work. %> <%= name 'this prints the variable name. This script will fail. %>
The // comment characters are supported in
JScript. These characters should be used on each comment line.
<% Call PrintDate %>
<SCRIPT LANGUAGE=JScript RUNAT=Server>
function PrintDate()
{
var x
x = new Date()
Response.Write(x.getDate())
}
// This is a definition for the procedure PrintDate.
// This procedure will send the current date to the client-side browser.
</SCRIPT>