Please follow me to learn some of the basics of using scripting languages (mainly VBScript) in ASP.
Before you begin to learn the scripting language, you should understand some simple concepts--variables, processes. A variable is a named storage location in the computer's memory that contains data such as numbers or strings, which makes it easy for users to understand the name of the script operation and provides a way for the user to store, retrieve, and manipulate the data. A program is made up of one or more processes, and in VBScript, a process is a "command block," usually a process such as a Sub, just for simple data processing.
In VBScript, a variable is strictly not declared,
Such as:
However, even if you don't need to declare a variable before you use a variable, you should develop a good habit of declaring variables at programming time, because it helps prevent errors from happening. Declaring a variable means telling the script engine that there is a variable of a particular name, so that the variable can be referenced in the script. Declaring a variable in VBScript allows you to use the Dim statement as follows:
< script language= "VBScript" >
<!--
Option Explicit ' requires all variables to be declared in the script
Dim Mystring
Mys Tring= "This are my string"-
->
</script>
The scope of the variable, which is the lifetime, determines which script commands can access the variable. Variables declared within a procedure have local scope. Each time a procedure is executed, the variable is created and then extinct. None of the commands outside the procedure can access it. Variables declared outside the procedure have global scope, and their values can be accessed and modified by any script command on the ASP page. Local variables and global variables can have the same name when declaring a variable. Changing one of the values does not change the value of the other. If you do not declare a variable, you may inadvertently change the value of a global variable. For example, the following script command returns a value of 1, although there are two named Y variables:
<%
Dim YY = 1Call Setlocalvariableresponse.write y
Sub setlocalvariable
Dim y
= 2End Sub % >
Because the variable is not explicitly declared, the following script command returns 2. When the procedure call sets Y to 2 o'clock, the script engine considers the procedure to be modifying a global variable:
<%
y = 1Call setlocalvariableresponse.write y
Sub setlocalvariable
y = 2 end
sub% >
However, global variables are available only in a single ASP page, and to make it available outside a single ASP page, you must give the variable a session or application scope. Session scope variables are available to all pages in the ASP application that a user requests. The same is true for application scope variables. For a single user, session variables are the best way to store information, such as user preferences, user names, or the identity of a user. For all users of a particular application, the application scope is the best way to store information, such as application-specific greetings or the initial values required by the application. ASP provides two built-in objects to store variables: Session objects and application objects, which will be highlighted in future ASP-built objects.
Let's take a look at the definition of a constant, which is used instead of a number or string of names, and it stays the same throughout the script. You can use the Const statement to create user-defined constants in VBScript. Use the Const statement to create a string or numeric constant with a certain meaning to the name, and to assign them a literal value. For example:.
such as :<% Const mystring= "This is a constant"% >
<% Const myage=100% >
Note that the string literal is contained between two quotes (""). This is the most obvious way to differentiate between string and numeric constants. The date text and time text are contained between two pound numbers (#). For example:
<% Const Cutoffdate = #6 -1-97#% >
After understanding the constants and variables, let's look at what the process is. It is a set of script commands that can perform a specified task and have a return value. You can define your own procedures and then call them repeatedly in your scripts. You can place a procedure definition in an. asp file of the calling procedure, or you can place a common procedure in a shared. asp file, and then include it in an SSI #include directive to another. asp file that calls its procedure. You can also choose another way to package these features in an ActiveX component. Procedure definitions can appear inside < script > and </SCRIPT > tags and must follow the rules for declaring scripting languages. If the procedure uses a different language than the main scripting language, use the < script > element. Procedures in the main scripting language are delimited by script delimiters (<% and% >). When you use the html< script > tag, you must use two properties to ensure that the server side can handle the script. The syntax for using the < SCRIPT > tag is as follows:
< SCRIPT runat=server language=jscript >
procedure Definition
</SCRIPT >
The Runat=server property here notifies the WEB server to process the script on the server. If this property is not set, the script is processed by the client browser. The LANGUAGE property determines the scripting language used for this script block. You can specify any language that has a script engine. Use VBScript to specify VBScript, and to specify JScript with JScript. If you do not set the LANGUAGE property, the script block is interpreted in the main scripting language.
In VBScript, processes are divided into two categories: Sub processes and Function processes. A Sub procedure is a set of VBScript statements that are contained between a sub and an End Sub statement, performing an action but not returning a value. Sub procedures can use parameters (constants, variables, or expressions passed by the calling procedure). If the Sub procedure has no arguments, the sub statement must contain an empty bracket ().
A Function procedure is a set of VBScript statements that are contained between a function and an End Function statement. A Function procedure is similar to a Sub procedure, but a Function procedure can return a value. A Function procedure can use parameters (constants, variables, or expressions passed by the calling procedure). If the Function procedure has no arguments, the function statement must contain an empty bracket (). A Function procedure returns a value that is assigned to a function name in the statement of a procedure by its name. The data type of a Function return value is always a Variant. In the following example, the Sub procedure uses two intrinsic (or built-in) VBScript functions, namely MsgBox and InputBox, to prompt the user for information. It then displays the results calculated based on this information. The calculation is done by the Function procedure created using VBScript, and the Celsius function converts Fahrenheit to degree Celsius. Sub procedure ConvertTemp When this function is called, the variable containing the parameter value is passed to the function. The conversion result is returned to the calling procedure and displayed in a message box.
Sub converttemp ()
temp = InputBox ("Please enter Fahrenheit temperature.") ", 1)
MsgBox" Temperature is "& Celsius (temp) &" Celsius. "End
Sub
function Celsius (fdegrees)
Celsius = (fDegrees-32) * 5/9 end
function
The way to pass data to a process is to use parameters. The parameter is used as a placeholder for the data to be passed to the procedure. The parameter name can be any valid variable name. When you create a procedure by using a SUB statement or Function statement, the procedure name must be followed by parentheses. The parentheses contain all the parameters, separated by commas. For example, in the following example, Fdegrees is a placeholder for the value passed to the Celsius function:
function Celsius (fdegrees)
Celsius = (fDegrees-32) * 5/9 end
function
To get data from a procedure, you must use a Function procedure. Keep in mind that a Function procedure can return a value, and a Sub procedure does not return a value.
The above gives you a brief introduction to VBScript, because of the length of the reasons, it is impossible to give a detailed description of VBScript all knowledge. However, because the ASP itself is not a programming language, so in the process of writing ASP applications, you must use scripting language to achieve many special features, so a flexible mastery of scripting language is essential for a programmer who writes Web applications using ASP.
Whether you are a master of rich programming experience or a beginner, as long as you now want to write a Web application through ASP, the small is strongly recommended that you master at least one scripting language (such as VBScript).