ASP Syntax You can not view ASP code in a browser, you will
only see the output from ASP which is plain HTML. This is because the scripts are executed
on the server before the result is sent to the browser.
In our school, every example displays the
hidden ASP code. This will make it easier for you to understand how it works.
The Basic Syntax Rule
An ASP file normally contains HTML tags,
just as a standard HTML file. In addition, an ASP file can contain server scripts,
surrounded by the delimiters <% and %>. Server scripts are executed on the server,
and can contain any expressions, statements, procedures, or operators that are valid for
the scripting language you use.
The Response Object
The Write method of the ASP Response
Object is used to send content to the browser. For example, the following statement
sends the text "Hello World" to the browser: Response.Write("Hello
World").
VBScript
In ASP it is possible to use different
scripting languages. The default language in ASP is VBScript, as in this example:
<html>
<body>
<%
response.write("Hello World!")
%>
</body>
</html> |
The example above uses the response.write
function to write Hello World! into the body of the HTML document.
JavaScript
To use JavaScript as the default
scripting language, insert a language specification at the top of the page:
<%@
language="javascript" %>
<html>
<body>
<%
Response.Write("Hello World!")
%>
</body>
</html> |
Note that - unlike VBScript - JavaScript
is case sensitive. You will have to write your ASP code with uppercase letters and
lowercase letters when the language requires it.
Other Scripting Languages
ASP comes with VBScript and JScript,
Microsoft's implementation of Javascript. If you want to script in another language, like
PERL, REXX, or Python, you have to install scripting engines for them.
Because the scripts are executed on the
server, the browser that requests the ASP file does not need to support scripting.
ASP Variables
A variable declared outside a
procedure, can be changed by any script in the ASP file. A variable declared inside a
procedure, is created and destroyed every time the procedure is executed.
Lifetime of Variables
A variable declared outside a procedure
can be accessed and changed by any script in the ASP page in which it is declared.
A variable declared inside a procedure is
created and destroyed every time the procedure is executed. No scripts outside that
specific procedure can access or change that variable.
To make a variable accessible to several
ASP pages, declare it either as a session variable or as an application variable.
Session Variables
Session variables store information about
one single user, and are available to all pages in one application. Common information
stored in session variables are username and userid. To create a session variable, store
it in a Session Object.
Application Variables
Application variables are also available
to all pages in one application. Application variables are used to hold information about
all users in a specific application. To create an application variable, store it in an
Application Object.
ASP Procedures
In ASP you can call a JavaScript
procedure from a VBScript and vice versa.
Procedures
ASP code can contain procedures and
functions:
<html>
<head>
<%
sub vbproc(num1,num2)
response.write(num1*num2)
end sub
%>
</head> <body>
The result of the calculation is: <%call vbproc(3,4)%>
</body>
</html> |
Insert the <%@
language="language" %> line above the <html> tag if you want to write
procedures or functions in a scripting language other than the default:
<%@
language="javascript" %>
<html>
<head>
<%
function jsproc(num1,num2)
{
Response.Write(num1*num2)
}
%>
</head> <body>
The result of the calculation is: <%jsproc(3,4)%>
</body>
</html> |
Calling a Procedure
When calling a VBScript or a JavaScript
procedure from an ASP page written in VBScript, you can use the "call" keyword
followed by the procedure name. If a procedure requires parameters, the parameter list
must be enclosed in parentheses when using the "call" keyword. If you omit the
"call" keyword, the parameter list must not be enclosed in parentheses. If the
procedure has no parameters, the parentheses are optional.
When calling a JavaScript or a VBScript
procedure from an ASP page written in JavaScript, always use parentheses after the
procedure name.
Page 1
2
3 4
5 |