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

Getting an array var from asp

Status
Not open for further replies.

Fuzemmo

Programmer
May 16, 2002
52
0
0
US
I have an array in my asp code that I want to access in a javascript. I found an example of doing this, but it was for a non-array var. I can access an individual array element via:

Code:
var myVar = "<%=arrImages(0)%>";

I can also access the "count" for the array, so I can get the entire thing with a loop. But is there a way for me to get the entire array in one hit? I've tried the following:

Code:
var myArray = Array()
myArray = "<%=arrImages%>"
or
myArray = "<%=arrImages()%>"

Neither of those seemed to work.

Any suggestions?
 
I'm using vbscript in my code-behind. I know its ugly, but I ended up using a work-around of converting my array in the code-behind to a pipe-delimitted string, and then just pulling that value over and parsing it back into an array format in the javascript. (Lord have I got a long ways to go.)
 
In other words, you did it exactly the way Dan showed, except in vbscript instead of javascript?

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
jaustin4
Another method I have used is to build the javascript array as I get the data.
In this example I am pulling the data from a recordset but you can apply the same concept to either writing the content of your ASP array to a javascript one, or creating just the javascript side one as you get the data in the first place.

' Write the javascript header
response.write "<SCR" & "IPT LANGUAGE=""JavaScript"">" & vbCrlf
' then declare the array
response.write "var assigneelist = new Array();" & vbCrlf
out="assigneelist = ["
do until rs.eof
' Store all Assigneed info for selected group in Client Side Javascript array assigneelist
If cnt <> 0 then
out = out & ","
End if
cnt=cnt+1
out = out & "'" & rs(0) & "','" & rs(1) & "','" & rs(2) & "','" & rs(3) & "','" & rs(4) & "'"
rs.movenext
loop
out = out & "];" & vbCrLf
response.write out
' write the script trailer...
Response.Write "</SCR" & "IPT>" & vbCrlf


Essentially you just create a string that is a javascript block instantiating the array then write it out.


Paranoid? ME?? WHO WANTS TO KNOW????
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top