Combining VBScript and HTML in ASP
- The intention of this example is to show how to mix VBScript and HTML and how to embed the results of the VBScript code in the HTML destined for the client.
- The <% @LANGUAGE=VBScript %> directive is used to set the scripting language for the entire page and must be the first line in the page. The default scripting language in IIS is set to VBScript. However, some users may alter this.
- The Option Explicit forces the declaration of all variables used within whatever scope they are used in. This ensures all variables are traceable and that there is no danger of conflict.
- Modern browsers and Web proxies cache web pages. Cacheing is the process of storing a quantity of web content such that subsequent visits do not require repeated downloading. If web page content changes frequently this is not an advantage. Expiration dates can be set for web pages. Also these web page expiration commands must be placed before any web page content text or tags. These expiration commands send an HTTP header to the browser. HTTP (Hypertext Transfer Protocol) headers are used from both client and server to send additional information and receive information in addition to the message body.
- Response.Expires = 0 causes a web page to expire immediately. Thus this web page would not be cahed at all.
- Response.ExpiresAbsolute = #January 1,2000 00:00:00# would cause a web page would be out of date on January, 1st 2000. Thus the page would be cached until that date and then overwritten.
- Time contains the current system time. DateAdd and DateDiff increment and find a difference of dates respectively; in this case as an hourly value.
- There is an available abbreviation of <% Response.Write(text) %> which can be replaced with <%= text %>.
- The constant VbCrLf adds a carriage-return-linefeed.
ASP allows the manipulation of every aspect of HTML code or text during the creation of an ASP web page.
<% @LANGUAGE = VBScript %>
<% Option Explicit %>
<%
Response.Expires = 0 ' in minutes
Dim dtmTime, dtmLater, dtmDiff
dtmTime = Time
dtmLater = DateAdd("h",1,dtmTime)
dtmDiff = DateDiff("h",Now,#1/1/2000#)
%>
<HTML>
<BODY>
Hello User.<BR>
The server's time is: <%= dtmTime %>.<BR>
In one hour the server's time will be: <%= dtmLater %>.<BR>
<%
If dtmDiff > 0 Then
Response.Write "Still " & dtmDiff & " hours "
Response.Write "to go till year 2000.<BR>" & VbCrLf
ElseIf dtmDiff < 0 Then
Response.Write "Already " & Abs(dtmDiff) & " hours passed since "
Response.Write "the beginning of the year 2000.<BR>" & VbCrLf
Else
Response.Write "The year 2000 has just begun.<BR>" & VbCrLf
Response.Write "<B>HAPPY NEW YEAR !</B><BR>" & VbCrLf
End If
%>