ASP 101 - Active Server Pages 101 - Web04
The Place ASP Developers Go!

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

 Top ASP 101 Stories Top ASP 101 Stories
Connections, Commands, And Procedures
What is ASP?
VBScript Classes: Part 1 of N

ASP Code-Based Security
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates

Quick Tips

Use Server-Side Script to Write Client-Side Script

It's often quite useful to be able to utilize a value from a server-side variable in your client-side scripting. While it may seem like a complex task, it's actually quite easy.

Normally the output from your ASP is in the form of HTML, but who says it has to be? There's absolutely no reason your ASP can't send client-side javascript to your browser just as easily. The following example takes the value of an VBScript variable on the server and sends it down to the client by popping it right into the appropriate place in a client-side confirmation script.

<%@ Language="VBScript" %>
' Let's say we have a variable which holds a value
' indicating someone's age that we got from a
' database or by performing some other calculation.
' For this example I'm just hard coding in a value.
intAge = 26
' In order to utilize this in client-side script we
' need to pass the value from the server-side code
' to the client-side code.  We do this by
' Response.Writing it out just like we would if we
' wanted to display it.  The only difference is that
' we place it into the appropriatre place inside of
' the client-side script instead of just in the HTML.
  <title>Server-Side Script to Client-Side Script</title>
  <script language="javascript">
    function btnSubmit_OnClick() {
      if (document.frmAge.txtAge.value != <%= intAge %>) {
        if (confirm('We calculated your age as <%= intAge %>.'
          + '  Are you sure you want to proceed?')) {
          return true;
        else {
          return false;
    // -->
<body bgcolor="#FFFFFF">
<form id="frmAge" name="frmAge">
  <input type="text" id="txtAge" name="txtAge" />
  <input type="submit" id="btnSubmit" name="btnSubmit"
    value="Submit" onclick="return btnSubmit_OnClick()" />

Previous      Show All Tips      Next

If you have a tip you would like to submit, please send it to:

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers