By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,776 Members | 1,397 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,776 IT Pros & Developers. It's quick & easy.

ASP variables concatenated in js function

P: n/a
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[i] = eval('<%'+'=Session("iCount'+i+'")%>');
alert(sessionsArray[i]);
}
}
</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>

Jul 23 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"David" <ri***@dd.com> wrote in message
news:%A31e.13937$wL6.13581@trnddc03...
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[i] = eval('<%'+'=Session("iCount'+i+'")%>');
alert(sessionsArray[i]);
}
}
</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[i]);
}
}
</script>
</head>
<body onload="alertIt()">
</body>
</html>
Jul 23 '05 #2

P: n/a
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" <Ne**@McKirahan.com> wrote in message
news:Np********************@comcast.com...
"David" <ri***@dd.com> wrote in message
news:%A31e.13937$wL6.13581@trnddc03...
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[i] = eval('<%'+'=Session("iCount'+i+'")%>');
alert(sessionsArray[i]);
}
}
</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[i]);
}
}
</script>
</head>
<body onload="alertIt()">
</body>
</html>

Jul 23 '05 #3

P: n/a
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[i]);
}
}
</script>
</head>
<body onload="alertIt()">
</body>
</html>
Jul 23 '05 #4

P: n/a
"David" <ri***@dd.com> wrote in message
news:Tr41e.25984$oa6.22571@trnddc07...
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(",");
Jul 23 '05 #5

P: n/a
> [snip]

Try:

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

Your a genuis :-)

David
Jul 23 '05 #6

P: n/a
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);

Jul 23 '05 #7

P: n/a
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

"humbads" <hu*****@gmail.com> wrote in message
news:11********************@l41g2000cwc.googlegrou ps.com...
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);

Jul 23 '05 #8

P: n/a
David wrote on 26 mrt 2005 in comp.lang.javascript:
"humbads" <hu*****@gmail.com> wrote in message
news:11********************@l41g2000cwc.googlegrou ps.com...
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);

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;s essionvarcnt++){
Response.write("'" + Session("iCount"+sessionvarcnt) + "'");
if(sessionvarcnt<maxsessionvar)Response.write(",") ;
};
%>);

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

</script>

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

not tested
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #9

P: n/a
I tested it and it does work .. interesting.
David

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;s essionvarcnt++){
Response.write("'" + Session("iCount"+sessionvarcnt) + "'");
if(sessionvarcnt<maxsessionvar)Response.write(",") ;
};
%>);

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

</script>

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

not tested
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.