Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i send a variable from asp (server) to client

Status
Not open for further replies.

BEHZADTALA

Programmer
May 28, 2003
43
DE
Dear Freinds
how i can send a variable(from asp page) to client(html or javascript)

thanks
 
Can you be a bit more specific? Do you mean a server variable, session variable, global variable or a variable you've declared in an ASP page?
 
the asp read a variable from textfile and then i want
to use this variable in my html page.
 
Do you mean you want to use ASP to read a text file and then return the results in an HTML format?
 
Q: how i can send a variable(from asp page) to client(html or javascript)

A: Response.Write
 
i want to use the variable from ASP in client javascript.
 

Code:
<%
dim sVar : sVar = "Hello World"
Response.write("Server Generated Var: " & sVar)
%>
<script type="text/javascript">
  var vJSVar = '<%=sVar%>';
  alert('Client Generated Var: '+vJSVar);
</script>

A smile is worth a thousand kind words. So smile, it's easy! :)
 
I was looking for something on this myself. From what I learned I don't think it's directly possible - but DNG will probably be able tell you for sure.

One work-around is to use the ASP variable to populate the value of a hidden field and then reference the hidden field in your Javascript.

e.g.
Code:
<form name="sneakyform">
<input type="hidden" name="amount" value="<% = intAmount %>"
</form>

and then you can reference the hidden field

Code:
document.sneakyform.amount.value
 
Dang! Damber posted while I was typing my reply. Try Damber's way first - looks a lot tidier, although I had problems when attempting something similar (but that's no suprise when I'm involved).
 
Just in case it wasn't clear in my last post:

Code:
<%
dim sVar : sVar = "Hello World"
Response.write("Server Generated Var: " & sVar)
%>
<script type="text/javascript">
  var vJSVar = '[highlight]<%=sVar%>[/highlight]';
  alert('Client Generated Var: '+vJSVar);
</script>

Put this into a test page (with the usual tags etc) and open the page - you should see the effect of the code.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
thank you
and how can i call asp without click submit.
for example when page is loaded or in javascript
 
ASP runs at the server before the page is sent to the client - therefore the bits inside <% %> will not be visible - BUT the output of these bits are, so the text that is sent to the client will look like:

Code:
<script type="text/javascript">
  var vJSVar = 'Hello World';
  alert('Client Generated Var: '+vJSVar);
</script>

Therefore the variable from ASP is already in the Javascript and available to use.

If you want to be able to call ASP functions from the client side AFTER the page has been loaded, then you can either reload the page, or you can use XMLHTTP (AJAX) - but think simple, don't make it more complicated than it needs to be.

A smile is worth a thousand kind words. So smile, it's easy! :)
 

Just in case it isn't clear - the javascript code gets a copy of the variables value - so you cannot change it on the client side only - you have to communicate with the server if you want to change it.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top