ASP   «Prev  Next»

Lesson 3 ASP script syntax
ObjectiveExamine an ASP script

Classic ASP Script Syntax

As we examined the server's and browser's treatment of ASP code within an HTML page, you may have noticed some unfamiliar characters. These delimiters[1], <% and %>, tell the interpreter where an ASP script begins and ends. ASP, as installed, has the ability to interpret either VBScript or JScript code. VBScript is the default, although you can direct ASP to recognize JScript code in an HTML page.

Inserting JScript into an HTML page

Although VBScript is the default scripting language for ASP, you can use other scripting languages, like JScript.
Place the HTML tags
  1. <SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT> and
  2. </SCRIPT>,
at the beginning and end of the JScript code.
For example:
  
<HTML>
<BODY>
   .
   .
<SCRIPT RUNAT=SERVER LANGUAGE=JSCRIPT>
 '
 ' JScript code goes here
 '
</SCRIPT>
  .
  .
</BODY>
</HTML>

In this course we will work in VBScript, and ASP scripts will be assumed to follow VBScript syntax and coding rules. Here is a simple example of an ASP script that stores data and then writes out that data when requested:


<HTML>
  <TITLE>This is an example</TITLE>
  <BODY>
  <%'BEGIN ASP SCRIPT
    Dim cVariable 'Initialize a character variable
    cVariable = "This is a variable in ASPScript"
    response.write cVariable
    %>
  <!–END ASP SCRIPT ––>
</BODY>
</HTML>

The HTML code that is sent to the browser will be:
<HTML>
  <TITLE>This is an example</TITLE>
  <BODY>
    This is a variable in ASPScript
  </BODY>
</HTML>

And the browser will display:

This is a variable in ASPScript

Note the use of standard HTML tags (HTML, TITLE, BODY), the ASP script bounded by its delimiters <% and %>, and the two styles of comments. The ASP interpreter will evaluate every expression between the delimiters. The client browser displays the results and messages that are generated from the ASP script. The following example performs a calculation and displays the result by inserting a calculated value into a line of text.
<HTML>
<TITLE>Applied Discount</TITLE>
<BODY>
<%             'BEGIN ASP SCRIPT
Dim nTotal     'Initialize a numeric variable
nTotal = 20    'Set this numeric variable to a value
%>             <!–END ASP SCRIPT ––>
Your $<%=nTotal%> total with a 10% discount is $<%=nTotal – (nTotal * .10)%>!
</BODY>
</HTML>


Output Value <%=

Placing an equals sign after the starting ASP script delimiter (<%=) is one way to direct ASP to output a value or text string. We will discuss ways to write in ASP in a later lesson. The output from this example will be:
Your $20 total with a 10% discount is $18!

  • Dynamic HTML Generation:
    This is a simplified illustration of dynamically-generated HTML code.
    In this example, a specific value is placed into a variable:
     nTotal = 20
    

    and a fixed discount percentage (10%) is used in the calculation.
    A more complex version of this same technique can be used in a Web application to take a previously-calculated value (for example, total amount of goods selected in a Web storefront) and perform the same discount calculation (even using a variable discount calculated from total amount of purchase), then display the result to the user to accept or "put back" one or more items that were purchased.
    We would display different results for different users who each end up with a different total amount and a different discount amount. The actual HTML code is created at the moment the calculation is completed.

Notice that we are performing a calculation in the middle of a line just before it is sent to the browser!

Local Server

You can test ASP pages on your local server by entering this as your address in your browser:
           
http://localhost/pagename.asp
where pagename is the filename you used to save the ASP page you created.
If you are using a server other than your local machine use the server's address instead (http://servername/pagename.asp).
The next lesson will explain how to create and put data into constants and variables.
[1]Delimiter: A character that separates one datum from the next.

SEMrush Software