Interacting with Client-Side Scripts

ASP's effectiveness can be extended by using it to generate or manipulate client-side scripts. For example, you can write server-side scripts that assemble client-side scripts based on server-specific variables, a user's browser type, or HTTP request parameters.

By interspersing server-side script statements within client-side scripts (enclosed by HTML <SCRIPT> tags), as shown in the following example template, you can dynamically initialize and alter client-side scripts at the request time:

  <SCRIPT LANGUAGE="VBScript">   
<!--

variable = <%=server defined value %>
.
.
.

client-side script

<% server-side script used to generate a client-side statement %> 

client-side script
.
.
.
-->
</SCRIPT>

Incorporating such functionality can produce some useful and interesting applications. For example, the following is a simple server-side script (written in VBScript) that manipulates a client-side script (written in JScript):

  <%
  Dim dtmTime, strServerName, strServerSoftware, intGreeting
 
  dtmTime = Time()
  strServerName = Request.ServerVariables("SERVER_NAME") 
  strServerSoftware = Request.ServerVariables("SERVER_SOFTWARE") 

  'Generate a random number.        
  Randomize
  intGreeting = int(rnd * 3)
%>

  <SCRIPT LANGUAGE="JScript">
  <!--

  //Call function to display greeting
  showIntroMsg()

  function showIntroMsg()
  {  
    switch(<%= intGreeting %>)
    {
    case 0:
      msg =  "This is the <%= strServerName%> Web server running <%= strServerSoftware %>."
      break
    case 1:         
      msg = "Welcome to the <%= strServerName%> Web server. The local time is <%= dtmTime %>."
      break
    case 2:
      msg = "This server is running <%= strServerSoftware %>."
      break
    } 

  document.write(msg)

  }

-->
</SCRIPT>

Scripts of this kind can be expanded, for example, to configure a client-side database or a DHTML personalization script. Innovative use of this technique can also reduce round-trips and server processing.