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



Windows Technology Windows Technology
15 Seconds
4GuysFromRolla.com
ASP 101
ASP Wire
VB Forums
VB Wire
WinDrivers.com
internet.commerce internet.commerce
Partners & Affiliates
ASP 101 is an
internet.com site
ASP 101 is an internet.com site
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
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

QUICK TIP:
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.
%>
<html>
<head>
  <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;
        };
      };
    }
    // -->
  </script>
</head>
<body bgcolor="#FFFFFF">
<form id="frmAge" name="frmAge">
  Age:
  <input type="text" id="txtAge" name="txtAge" />
  <input type="submit" id="btnSubmit" name="btnSubmit"
    value="Submit" onclick="return btnSubmit_OnClick()" />
</form>
</body>
</html>

Previous      Show All Tips      Next

If you have a tip you would like to submit, please send it to: webmaster@asp101.com.


Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

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