ASP variables concatenated in js function

D

David

Howdy ... having trouble here

I have 3 session variables in ASP.

Session("iCount1")
Session("iCount2")
Session("iCount3")

I need to concatenate these in a javascript function.

<script type="text/javascript">
function alertIt(){
var sessionsArray = new Array();
for(var i=0;i<3;i++){
sessionsArray = eval('<%'+'=Session("iCount'+i+'")%>');
alert(sessionsArray);
}
}
</script>

Doesn't work. I've tried all kinds of syntax but am in need of some advanced
knoweledge at this point.

David

PS: This works below, a straightforward string of one of the session vars.

<script type="text/javascript">
function alertIt(){
var theSess = '<%Response.Write(Session("iCount2"))%>';
alert(theSess);
}
</script>
 
M

McKirahan

David said:
Howdy ... having trouble here

I have 3 session variables in ASP.

Session("iCount1")
Session("iCount2")
Session("iCount3")

I need to concatenate these in a javascript function.

<script type="text/javascript">
function alertIt(){
var sessionsArray = new Array();
for(var i=0;i<3;i++){
sessionsArray = eval('<%'+'=Session("iCount'+i+'")%>');
alert(sessionsArray);
}
}
</script>

Doesn't work. I've tried all kinds of syntax but am in need of some advanced
knoweledge at this point.

David

PS: This works below, a straightforward string of one of the session vars.

<script type="text/javascript">
function alertIt(){
var theSess = '<%Response.Write(Session("iCount2"))%>';
alert(theSess);
}
</script>



An alternative is:

<%@ Language="VBScript" %>
<%
Session("iCount1") = 1
Session("iCount2") = 2
Session("iCount3") = 3
Dim i, a(), s
For i = 0 To 2
ReDim Preserve a(i)
a(i) = Session("iCount"&i+1)
Next
s = Join(a,",")
%>
<html>
<head>
<title>Sessions.asp</title>
<script type="text/javascript">
function alertIt() {
var sessionsArray = new Array(<%=s%>);
for (var i=0; i<3; i++) {
alert(sessionsArray);
}
}
</script>
</head>
<body onload="alertIt()">
</body>
</html>
 
D

David

Thanks, very nice. That does work however the session variables in this case
are strings. I found that I can get the string by modifying this..

Session("iCount1") = "number 1"
Session("iCount2") = "number 2"
Session("iCount3") = "number 3"

var sessionsArray = new Array(<%=s%>);
to this...
var sessionsArray = new Array('<%=s%>');

but then the alert brings up the array joined on the first alert, and the
following 2 alerts come up as undefined.

number1,number2,number3

What do you think?

David



McKirahan said:
David said:
Howdy ... having trouble here

I have 3 session variables in ASP.

Session("iCount1")
Session("iCount2")
Session("iCount3")

I need to concatenate these in a javascript function.

<script type="text/javascript">
function alertIt(){
var sessionsArray = new Array();
for(var i=0;i<3;i++){
sessionsArray = eval('<%'+'=Session("iCount'+i+'")%>');
alert(sessionsArray);
}
}
</script>

Doesn't work. I've tried all kinds of syntax but am in need of some advanced
knoweledge at this point.

David

PS: This works below, a straightforward string of one of the session vars.

<script type="text/javascript">
function alertIt(){
var theSess = '<%Response.Write(Session("iCount2"))%>';
alert(theSess);
}
</script>



An alternative is:

<%@ Language="VBScript" %>
<%
Session("iCount1") = 1
Session("iCount2") = 2
Session("iCount3") = 3
Dim i, a(), s
For i = 0 To 2
ReDim Preserve a(i)
a(i) = Session("iCount"&i+1)
Next
s = Join(a,",")
%>
<html>
<head>
<title>Sessions.asp</title>
<script type="text/javascript">
function alertIt() {
var sessionsArray = new Array(<%=s%>);
for (var i=0; i<3; i++) {
alert(sessionsArray);
}
}
</script>
</head>
<body onload="alertIt()">
</body>
</html>
 
D

David

This seems to work as I need it but I'm sure there's a more elegant method.
If not thanks, you have helped me alot.



<%@ Language="VBScript" %>
<%
Session("iCount1") = "number 1"
Session("iCount2") = "number 2"
Session("iCount3") = "number 3"
Dim i, a(), s, b
For i = 0 To 2
ReDim Preserve a(i)
a(i) = Session("iCount"&i+1)
Next
s = Join(a,"~")
%>
<html>
<head>
<title>Sessions.asp</title>
<script type="text/javascript">
function alertIt() {
var sessionsArray = new Array('<%=s%>');
var sessionsArraySplit = sessionsArray.toString().split("~");
for (var i=0; i<sessionsArraySplit.length; i++){
alert(sessionsArraySplit);
}
}
</script>
</head>
<body onload="alertIt()">
</body>
</html>
 
M

McKirahan

David said:
Thanks, very nice. That does work however the session variables in this case
are strings. I found that I can get the string by modifying this..

Session("iCount1") = "number 1"
Session("iCount2") = "number 2"
Session("iCount3") = "number 3"

var sessionsArray = new Array(<%=s%>);
to this...
var sessionsArray = new Array('<%=s%>');

but then the alert brings up the array joined on the first alert, and the
following 2 alerts come up as undefined.

number1,number2,number3

What do you think?

David

[snip]

Try:

var sessionsArray = "<%=s%>".split(",");
 
H

humbads

If you always only have three variables, why use an array? Why not
something straightforward like this? (Note the ASP portion is in
VBScript below. Change ampersand to plus sign for Javascript.)

var sessConcatenated = '<%
Response.write(Session("iCount1") & ", ")
Response.write(Session("iCount2") & ", ")
Response.write(Session("iCount3"))
%>';
alert(sessConcatenated);
 
D

David

It's a little more complicated than that, The example I gave was extremely
condensed for legibility here in the forum. The number of sessions will be
different for every user and they could get quite large.

David
 
E

Evertjan.

David wrote on 26 mrt 2005 in comp.lang.javascript:
It's a little more complicated than that, The example I gave was
extremely condensed for legibility here in the forum. The number of
sessions will be different for every user and they could get quite
large.

If you want to use a clientside array:

=======================

<%@ language="vbscript"%>
<% ' Using serverside asp-vbscript and clientside javascript: %>

<script type='text/javascript'>

clientsidearray = new Array(<%
for sessionvarcount=1 to maxsessionvar
Response.write "'" & Session("iCount"&sessionvarcount) & "'"
if sessionvarcount<maxsessionvar then Response.write ","
next
%>);

alert( clientsidearray.join('#') );

</script>

=======================

<%@ language="javascript"%>
<% // Using serverside asp-jscript and clientside javascript: %>

<script type='text/javascript'>

clientsidearray = new Array(<%
for(sessionvarcnt=1;sessionvarcnt<=maxsessionvar;sessionvarcnt++){
Response.write("'" + Session("iCount"+sessionvarcnt) + "'");
if(sessionvarcnt<maxsessionvar)Response.write(",");
};
%>);

alert( clientsidearray.join('#') );

</script>

========================

not tested
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top