Download
Academy
Current location: Downcodes.com -> Academy -> Programming -> ASP tutorial -> Combining server and client side scripts
Recommend
HOT TOP10
Combining server and client side scripts
Date: 2008-1-4 Author: Hit: View:[Large font Middle font Small font]
This article will present the use of several techniques associated with scripting in an Active Server page. The objective is to show examples of how scripts can be used to combine client-side information with server-side information.

To achieve this result, a simple data form is used as an example to produce a timed-greeting message addressed to the user's name. The form is part of the ASP page and the page is going to be recursive, that is to say, it will resubmit to itself. The nice thing about this technique is that the page can determine its own name.

So, let's get started. Begin with the server collecting the name you have given to the page.


<%@ LANGUAGE="VBSCRIPT" %>

<%
    strScriptName = Request.ServerVariables("SCRIPT_NAME")
%>


Set up the usual opening tags for your page.


<HTML>
<HEAD>
<TITLE>First Script</TITLE>
<META name="description" content="">
<META name="keywords" content="">
<META name="generator" content="VisualN++">

</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">

<CENTER>
<H2>First Script</H2>


Create the form to collect the user input, and condition the form so it only shows on the first pass. This will be accomplished by adding a named-value to the URL.

Again, the URL being called by the form is the page itself. If this is the first pass through the page, the form will be rendered and the page will then stop. No additional information from the page is required until the user has submitted the desired input.


<%if request("Pass") <> 2 then%>
<Form Action="<%=strScriptName%>?Pass=2" Method="Post">
    <input type="text" name="yourname" value="Enter your name here"><BR><BR><BR>
    <input type="submit" value="Submit">  
    <input type="reset" value="Reset">
</Form>

<%
    Response.end
end if
%>


After the form is submitted by the user, and we are making a second pass through the page, the server will collect the input and assign it to a variable. Note that the server is not distinuishing the method of the request object. This shorthand technique can save a lot of errors when using combination of "Get" and "Post" methods.


<%
    Name = Request("yourname")
%>


Now get the user's local time from their machine with client-side script and display the opening phrase of the response. This script runs automatically as the page is being processed by the server.


<HR>

<script Language ="VBScript" ><!--
Response=""
    If Hour(Now) < 12                         then Response = "Good Morning"
    If (Hour(Now) >= 12 and Hour(Now) < 18)    then Response = "Good Afternoon"
    If (Hour(Now) >= 18 and Hour(Now) <=24)     then Response = "Good Evening"
document.write(response)
//-->
</Script>


Now display the input from the user, using the server-side value "Name" and another client-side script that will run automatically to process the local time.


<%=Name%>, it's now

<script Language ="VBScript" ><!--
    document.write (FormatDateTime(time,vblongtime))
//-->
</Script>


Finally, give the user some direction on how to proceed with a button that triggers an event-activated script.


<Script FOR="Repeat" EVENT="OnClick" Language="VBScript"><!--
    location.href= "<%=strScriptName%>"
--></Script>

<HR>

<BR><BR><BR>

<Button Name="Repeat">Click Here to Start Over</Button>

(From: aspkey)

Relative article:
Relative software: