ASP basics
ASP code can be declared in various places, be written in Javascript or VBScript, and can be used to print text as well as contain HTML tags.
This lesson focuses on:
- Declaring an ASP script within a webpage
- Including VBScript code
- Including Javascript code
- Printing text on a webpage
- Including HTML tags in a script
- Including comments in a script
- Special characters
Declaring an ASP script within a webpage
An ASP script begins with <% and ends with %>. It can be placed anywhere within a webpage. You can have as many scripts within a webpage as you want.
Syntax:
<% script contents go here %>
Example:
<html>
<head>
<title>ASP script</title>
</head>
<body>
<%
Response.Write "ASP is cool!";
%>
</body>
</html>
Output:
Including VBScript code
Since VBScript is the default language of ASP, you do not have to explicitly mention to ASP that you want to include VBScript code, you just do it as in the above example as well as in this example:
<html>
<head>
<title>ASP script written in VBScript</title>
</head>
<body>
<%
Response.Write "VBScript by default!
%>
</body>
</html>
Output:
Including Javascript code
Including Javascript code in ASP is different than including VBScript code. To include Javascript code in ASP you have to have a language specification at the top of a page that looks like this:
<%@ language="javascript"%>
Example:
<%@ language="javascript"%> <html> <head> <title>ASP script written in Javascript</title> </head> <body> <% Response.Write("Javascript and ASP!") %> </body> </html>
Output:
This language specification will tell ASP that a script is going to contain Javascript.
NOTE: The language specification should be the first line of code on the page or you will get an error.
NOTE: JavaScript is case sensitive unlike VBScript. You will have to be careful with uppercase and lowercase letters when including Javascript code in ASP.
Can ASP include other languages besides Javascript and VBScript?
Yes it can. But if you want to use any other languages besides Javascript and VBScript, you will have to install the necessary script engines for them.
NOTE: Javascript and VBScript are both client-side languages but when included in ASP, they are executed on the server-side.
NOTE: ASP does not include the general Javascript language, but JScript which is Microsoft's implementation of Javascript. Read more about JScript at Microsoft'sJScript page.
Printing text on a webpage
Printing text on a webpage with ASP is simple. To do so use the Response.Write() statement.
Syntax:
Response.Write("textToPrint")
Example:
<%
Response.Write("ASP stands for Active Server Pages")
%>
Output:
ASP stands for Active Server Pages
Alternatively, you could just use an equal sign instead of the Response.Write() statement:
<%="ASP stands for Active Server Pages"%>
Output:
ASP stands for Active Server Pages
Including HTML tags in a script
HTML tags can be included in a script by including them together with regular text. Any tags included in a script this way will be interpreted by the web browser as regular HTML.
Example:
<html>
<head>
<title>HTML tags in scripts</title>
</head>
<body>
<%
Response.Write("<b><i>This text will be bold and italic</i></b>")
Response.Write("<br /><a href='http://www.landofcode.com'>Landofcode.com main page</a>")
%>
</body>
</html>
Output:
Landofcode.com main page
Including comments in a script
Comments in ASP are declared so that code would be easier to understand and to navigate. Comments are not seen on a webpage, but only within the source code. Comments can be placed anywhere within ASP source code. In ASP you can have only single line comments, no multi-line comments. Single line comments in ASP are declared with the apostrophe ( ' ) symbol.
Example:
<html>
<head>
<title>Comments in ASP</title>
</head>
<body>
<?%
'this is a single line comment
'this is another single line comment
Response.Write("This script contains some comments")
%>
</body>
</html>
Output:
This script contains some comments
NOTE: Single line comments can span only a single line.
Special characters
ASP uses some special characters and knowing them is fundamental to writing scripts with ASP:
-
& - The "concatenation" operator.
This operator combines text strings.Example:
<% Response.Write("This text string " & "is combined.") %>Output:
This text string is combined. -
' - declares a comment
Example:
<% 'this line of text is a comment 'so is this one Response.Write("ASP is cool!") %>Output:
ASP is cool! -
_ - spans lines
Use the underscore character ( _ ) to span multiple lines of ASP code.Example:
<% Response.Write("This is a really " & _ "really really really really really really " & _ "long sentence.") %>Output:
This is a really really really really really really really long sentence. -
: - line extender
Use the colon character (:) to put multiple lines of code on one line. Although this is not recommended because it makes code harder to read.Example:
<% a = 1 : b = 2 : c = 3 : d = 4 %>
as opposed to:
<% a = 1 b = 2 c = 3 d = 4 %>
-
. - method caller
Use the period character ( . ) to call methods. Methods are a part of objects. An object is basically a collection of data stored in one set.Example:
<% Server.CreateObject("CDO.Message") %> -
% - code signifier
Use the percent symbol ( % ) to signify the beginning and end of ASP code.Example:
<% 'asp code goes here 'the percent symbol precedded by a < symbol starts the ASP code 'the percent symbol followed by a > symbol ends the ASP code %>
-
%= - code signifier shortcut
Use the percent symbol ( % ) together with the equal symbol ( = ) to signify the beginning of ASP code when you need to quickly print some text instead of using the Response.Write() statement.Example:
<% Response.Write("The longer way to print text") %> <%="Here is some text%>
Output:
The longer way to print text Here is some text




