ASP Lecture Series (13) Transferring Scripts to the Browser
Author:Eve Cole
Update Time:2009-05-30 19:58:58
Although ASP is primarily used to create and process server-side scripts, you can extend its usefulness by using it to generate client-side scripts that are processed by client browsers. Server-side scripts can be written by combining script commands sent to the browser.
This is done by combining client-side script enclosed by HTML comments and server-side script enclosed by delimiters:
<SCRIPT LANGUAGE="VBScript">
<!--
client script
<%serverscript%>
client script
<%serverscript%>
client script
...
-->
</SCRIPT>
Using this feature of scripting languages, you can create exciting applications. For example, the following script will generate a subroutine of a client script that runs on the user's Web browser.
<%
ServerTime = Time
ServerDate = Date
For i = 1 to 4
Randomize
GreetCondition = int(rnd * 3)
%>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub ServeInfo<%= i %>()
Select Case <%= GreetCondition%>
Case 0
Msg = "Hello, the time is <%= ServerTime %>."
Case 1
Msg = "Welcome! Today's date is <%= ServerDate %>."
Case 2
Msg = "Hi, the time is <%= ServerTime %> and the date is <%= ServerDate %>.
End Select
Document.Write Msg
End Sub
ServeInfo<%= i %>()
//-->
</SCRIPT>
<br>
<%
Next
%>
In the above script, ASP retrieves time and date information on the server and then loops through several times to generate a subroutine that runs on the user's Web server. Each client subroutine presents a randomly selected greeting and displays time and date information.
This script can be extended, for example, to retrieve and submit configuration information to a specified client script or component, such as an ActiveX control. Flexible use of this scripting technique can also speed up the Web server's processing and return of user information requests.