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

Passing records to javascript function

Status
Not open for further replies.

malaygal

IS-IT--Management
Feb 22, 2006
192
US
My asp page is retrieving some records from an Access database to be displayed. I have a javascript function on the same page that I want to pass these records to. How do I do that.
If I can't pass them to the function, how can I retrieve the same records within my javascript funtion.


Any help will be greatly appreciated.
 
Hi. The only way of doing this on the one page would be
<script>
var test = <%=vbvariable%>
</script>

So you actually have the asp write into your javascript.

Remember that the asp before the client sees the page, so the asp can not directly interact with the javascript

}...the bane of my life!
 
Generally in the past when I have needed to access the server-side data from the client I have written that data out as a Javascript array. Something like:
Code:
<%
' assuming our recordset is called rsData
Response.Write "<script language=""javascript"">" & vbCrLf

'start the array declaration
Response.Write "var data = new Array("

'move to the first record
If Not rsData.EOF Then rsData.MoveFirst

'loop through records
Dim col
Do Until rsData.EOF
   'start the nested array declaration
   Response.Write "new Array("

   'output all data for row
   For Each col in rsData.Fields
      Response.Write "'" & col & "',"
   Next

   'end nested array
   Response.Write "),";

   'move to next record
   rsData.MoveNext
Loop

'end array and script
Response.Write ");" & vbCrLf
Response.Write "</script>"
%>

What this will output will be all of your data from your recordset as a set of nested arrays. AS this was an example I didn't do anything fancy to check if we really needed comams or not, so you will have an extra (empty) row and an extra (empty) column in each row.

The inner for loop is actually less efficient the spelling out what you want and will output everything between single quotes to treat it as a string. If you have numeric data that you want to treat as number, or only wanted to output a specific set of your columns, you could replace that for loop with Response.Writes of the specific fields you want, with or without single quotes.

Hope this helps,
-T

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top