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!

pass javascript array 2

Status
Not open for further replies.

leahyj

MIS
May 5, 2004
107
US
Hi,

Can I pass from Asp an multidimensional array to javascript.

I had success passing the one dimesional:

Code:
Response.Write "<script type='text/javascript' language='javascript'>"
Response.Write "var liensArr = new Array();"
Response.Write "</script>"

later on:

Response.Write "liensArr" & "[" & i & "]" & "='" & rsLiens.fields("PropIntID").value & "';"

Thanks
 
You could try something like this:

Code:
Response.Write "<script type='text/javascript' language='javascript'>"
Response.Write "var liensArr = new Array();"

intRow = 0
Do While Not rsLiens.EoF
  Response.Write "liensArr [" & intRow & "] = new Array('" _
               & rsLiens("PropIntID") & "','" _
               & rsLiens("SomethingElse") & "','" _
               & rsLiens("AnotherField") & "');" & vbCrLf
  intRow = intRow + 1
  rsLiens.MoveNext
Loop

Response.Write "</script>"

Overall the key to writing the ASP will be to figure out how the JavaScript should be as a plain HTM file... then once you get it running as a plain HTML you tweak the ASP logic so it produces the desired JavaScript.
 
I frequently pass arrays of data back and forth between ASP and JavaScript by using join and split to flatten the array and "reinflate" it. I don't think I've tried it with a multidimensional array yet, but it might work.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Multidemensional arrays don't exsit in JScript or JavaScript only VBscript, you need to use VBArray object to convert.
 
So not to confuse you can have a javascript array with in another javascript array. An array within an array but this is not a multidim array.
 
I would suggest breaking up the output of the script tags. When ASP processes the page it can see the script tags and gets confused as to what it should be processing.
When I build javascript tags for output I concatenate the string before output so ASP will not ignore the section.

So rather than using:
Code:
Response.Write "<script type='text/javascript' language='javascript'>"
Response.Write "var liensArr = new Array();"

Response.Write "</script>"

I use:
Code:
Response.Write "<SCR" & "IPT type=""text/JavaScript"">" & vbCrlf
Response.Write "</SCR" & "IPT>" & vbCrlf

Generally speaking though you are better off to build the entire string for output and do a single response.write rather than lots of individual writes to the page. Response.Write will cause a performance hit and even if your application will not be outputting lots of data this time it is a good habit to get into so you do not run into problems later.


At my age I still learn something new every day, but I forget two others.
 
When ASP processes the page it can see the script tags and gets confused as to what it should be processing.

Interesting, I've never run into that before. Example or Doc?

 
I to would be interested in the documentation behind this!
I know it is a problem when Dynamically writing javascript tags in javascript
Code:
newWin.document.write("<body><scr" + "ipt src='includes\js.js'></scr" + "ipt><scr" + "ipt>alert('hello')</scr" + "ipt><div id='uploading'
If there is a problem with this I need to reassess 7 years of asp scripts!!!

}...the bane of my life!
 
If you are mixing VBScript and Javascript on the same page you can end up with nested script tag errors.
This generally happens if you have set a RUNAT=Server command or if you have declared the page to be VBScript then insert some dynamic Javascript in the page.

It can also happen if you use a javascript logic block to alter content that is loaded into the page that is contained within it's own script tags.

It does not happen in every situation but it is a good idea to output the script tag broken up to prevent any chance that ASP will interpret it literally rather than just rendering it on the page.

Look at this link:

Or do a search on ASP nested script error for other examples of what can cause the problem.

At my age I still learn something new every day, but I forget two others.
 
Interesting
Relates as you say to using serverside javascript more than anything, but somthing to keep in mind.
As I always have <% @language="vbscript" %> at the top of the page I doubt I would have seen this!!
Nice tip.

}...the bane of my life!
 
I have never used <% @language="vbscript" %> on my pages and do not use server-side javascript but I have run into the problem.
I do not recall the circumstances behind the issue as it was a couple of years ago but I found samples of code where the script tags were broken up and tried them and it resolved my problem. It was not until much later that I found out why the tags were broken up, I just knew that it resolved an issue I was having at the time with no good explanation.

I now break the tags up just as a precaution.


At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top