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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Increment parameter on server in JavaScript

Status
Not open for further replies.

Bakunin

Programmer
Dec 21, 2000
31
GB
I'm a recent convert to JavaScript and have a potentially simple question. I have a small script which populates an array from records returned from a DB. My question is simply, is it allowable for a server variable to be defined and incremented within JavaScript? I want to do this to increment the array index but the server variable keeps resetting each time a for loop iterates.

answers appreciated.

Bakunin.

function FillArray()
{
var DetailArray = new Array();
var i;
<% dim j
j = 1 %>

for(i=0; i <= document.formDelivery.delivery.length - 1; i++)
{
alert(i);
DetailArray= new Array(&quot;<%=var_array(0,j)%>&quot;,&quot;<%=var_array(1,j)%>&quot;);
alert(DetailArray);
alert(&quot;j = &quot; + <%=j%>);
<%j = j + 1 %>
alert(&quot;j = &quot; + <%=j%>);
}
}
 
Hmmm.. well you need to remember that once the client-side script begins executing, the server has already finished ALL of its parsing. So just write the server-variable into a client-side variable:

<script>
myvar=<%=serverversionofmyvar%>
</script>

then go ahead and use it normally jared@aauser.com
 
Like Jared said. And if you want to get the client-side variable back to the server again when the page is finished, you can pass it on the query string like this:

eval(&quot;window.location=file.asp?var=&quot;+your_variable);

Server-side and client-side scripting are seperate processes which can't interact directly on the same page. This may be a little confusing because you are using ASP and all of the code is on the same page. It is much clearer if you use CGI where your server-side code usually writes your client-side code to the browser rather than intermingling them.

In any event, you can do server-client-server-client stuff by passing the variables back and forth by reloading the page with the variable in the query string. You could also POST it to STDIN by using a form.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Jared / Tanderso,
thanks for the replies, both where useful. Tanderso, your point about reloading the page...is it not possible to simply load the contents from an array returned to a page from a DB using ASP and then populate a client side Javascript array? I don't want to make unessecary trips to server and back.

My problem is that I want to increment the index reference (i) in ASP with a client variable, to populate the client array. If trips to the server is the only solution I can live with it.

DetailArray= new Array(&quot;<%=var_array(0,i)%>&quot;,&quot;<%=var_array(1,i)%>&quot;);

since &quot;i&quot; is declared as a Javascript variable how will it behave inside ASP tags?

thanks

Bakunin.
 
No, you can't. By the time the client-side script is executed (on the user's browser), the server-side script is finished executing (on the server) and has sent the result. There is no longer any ASP code in the page when the client browser gets it, and the server has nothing to do with that page anymore. You would have to send it back to the server again if you want the server to get the variable. Do you understand that these are two seperate processes done on two seperate machines? Do a &quot;view source&quot; of your page from your web browser to see what I mean.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
tanderson, again thanks. I think I ( at least ) appreciate the concept. Done it using the following to get round my problem: Very dirty method but did the trick ( i.e. do the lot on the server ). I have to agree with your assertion regarding CGI, unfirtunately I am tied to ASP at the minute.


Bakunin.


%>
'-- Create an Array
Response.Write(&quot;<script Language='JavaScript'>&quot; &amp; vbcrlf)
Response.Write(&quot;function doIt() {&quot;)
Response.Write(&quot;var DetailArray = new Array();&quot;)
Response.Write(&quot;var j = 0;&quot;)

for i = 0 to ubound(var_array, 2)
'-- Put records into the two dimensional Array.
Response.Write &quot;DetailArray[j]= new Array(&quot; &amp; var_array(0,i) &amp; &quot;,'&quot; &amp; var_array(1,i) &amp; &quot;');&quot;
Response.Write &quot;j++;&quot;
next

Response.Write(&quot;</script>&quot;)
%>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top