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 to transfer ASP variable to JavaScript in the same page? 4

Status
Not open for further replies.

Mojojojo

Programmer
Jun 25, 2001
11
US
My question is actually more than the above.

I have an ASP variable that will be declared at the bottom of a page. I would like to print the value at the top of the page. It seems that I should be able to use javascript to pluck the value and print it at the top of the page since ASP is done on the server and only sends HTML to the client where JavaScript should be able to do it's magic.

I have tried many things. I would also like it to print the value without having to refresh or click anything. I was playing with body onload but have not been able to get it to work. Here is a layout of the page:

<html>
<head></head>
<body>
I would like to print the value of strCount here
...there is more cold and such here

<% strCount = &quot;10&quot; %> This is the ASP variable
</body>
<html>

Is this even possible? Thanks in advance ( I always vote positive for even remote help).

Joe
 
Hi mojojojo,

I think that the best way to go is this:

Code:
<html>
<head>
<% strCount = &quot;10&quot; %>
</head>

<body>
  <%=strCount%>
</body>
<html>[code]

I think this is the only workable and easiest sollution (If anyone disagrees on that, I'm eager to learn better ways :-P)

Hope it gets you further.

Gtz,

Kristof
 
Hi,

for the javascript, you can try,

<% strCount = &quot;10&quot; %>
<script>
var myVal = &quot;<%=strCount%>&quot;;
alert(myVal);
</script>

hope this helps, Chiu Chan
WebMaster & Software Engineer
emagine solutions, inc
cchan@emagine-solutions.com
 
Kristof,

I would do that except the ASP variable is not available until the bottom of the page. I can't assign it at the top. I was hoping JavaScript could read through the page and grab the value. I can assign the ASP variable easy enough to JavaScript, but I can't print the new JavaScript variable *above* (to the top of the page) the point where it is assigned it's value.

I also looked at placing the ASP variable in a hidden form field (again, this is at the bottom of the page). The form had no buttons or actions. I thought I could use JavaScript to grab the value of the field, but I am coming up empty.

Thanks for your help,

Joe
 
Hi,

try something like this, it works in IE, you would have to adjust for functionally challenged browsers:

Code:
<html>
<head>
<script type=&quot;text/javascript&quot;>
function writeit() {
document.all.diva.innerHTML=document.all.divb.innerHTML
}
</script>
</head>
<body onLoad=&quot;writeit();&quot;>
<div id=&quot;diva&quot;>
</div>
<%
myvar=&quot;10&quot;
response.write(&quot;<div id='divb' style='visibility:hidden'>&quot; & myvar & &quot;</div>&quot;)
%>
</body>
</html>

or

Code:
<html>
<head>
<script type=&quot;text/javascript&quot;>
function writeit() {
document.all.diva.innerHTML=texte;
}
</script>
</head>
<body onLoad=&quot;writeit();&quot;>
<div id=&quot;diva&quot;>
</div>
<%
myvar=&quot;10&quot;
response.write(&quot;<script type='text/javascript'>texte='&quot; & myvar & &quot;';</script>&quot;)
%>
</body>
</html>

If I think of other ways, I'll let you know.

Bye.
 
yahve,

What a great answer! Of course I voted you positive (and anyone else who lended me a helping hand).

Thank you very much,

Mojojojo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top