Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Creating a Javascript array from a VBScript Array

Posted on 2004-08-04
Medium Priority
Last Modified: 2012-05-05

In my ASP page I am reading some data (page abstract text) into an array, arrText as follows:

Dim rsAllSections
set rsAllSections = Server.CreateObject("ADODB.Recordset")
strSQL = "select ssecID, ssubsecID, ssecSection, ssubsecSubSection, iitemID, iitemAbstract from tvAllSecSub order by ssecHomepageOrder, xorder"
rsAllSections.Open strSQL, cnnConn, 1, 4

'Load the SubSections and abstracts into an array we can use later in JavaScript
dim arrNameAbs
arrNameAbs = rsAllSections.GetRows(,0,array("ssubsecSubSection","iitemAbstract", "ssecID", "ssubsecID"))

Dim strText, arrText
Dim a
For a = LBound(arrNameAbs,2) to UBound(arrNameAbs,2)
strText = strText & "<h1 class='mainHeading'>" & arrNameAbs(0,a) & "</h1><span class='smalltext'>" & arrNameAbs(1,a) & " <a href='subsection.asp?SectionID=" & arrNameAbs(2,a) & "&SubsectionID=" & arrNameAbs(3,a) & "'>More&raquo;</a></span>^"
strText = Replace(strText, chr(10), "")
strText = Replace(strText, chr(13), "")
arrText = split(strText, "^")

I write out a link for each record inb teh recordset created above.  When these links are rolled over, they call a Javascript function to change an iLayer and display the appropriate text from teh array.  The links look like this:

" <a href='subsection.asp?SectionID=2&SubsectionID=3' onMouseover='changetext(content[0])' class='smalllink'> "

My Javascript looks like this:

var content=new Array()

function regenerate()

function regenerate2()
      if (document.layers)

function changetext(whichcontent)

      if (document.all||document.getElementById)
            cross_el=document.getElementById? document.getElementById("descriptions"):document.all.descriptions
            cross_el.innerHTML='<font face="Verdana"><small>'+whichcontent+'<font></small>'
      else if (document.layers)
            document.d1.document.d2.document.write('<font face="Verdana"><small>'+whichcontent+'</small></font>')

function appear()


I currently have to manually write additional " content[0]="<%=arrText(0)%>" " lines into the page to make the functionality work for all the links that are written onto the page.  THis means that I can't remove content using an admin interface without manually editing the page in question.  The url of the page is if you want to see what I'm on about.

I want to change the Javascript so that the "content" array is dynamically created, depending on the amount of rows returned from the database.  To do this, I want to populate the Javascript array "content" from the vbScript array "arrText" - how canb I do this?
Question by:s9
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 63

Accepted Solution

Zvonko earned 1000 total points
ID: 11713913
Like this:

var content= [
<%  For a = 0 to UBound(arrText) %>
   "<%=arrText(a)%>" ,
<%  Next %>


Expert Comment

ID: 11713934
Hi Buddy

    Hope this helps you.

<%@ Language=VBScript %>
      dim strValue
    str=" var Content = new Array(); "
    for i=0 to ubound(arrText)
            str = str & " Content["& i &"] ='"& arrText(i) &"';"
      // Javascipt array
      <%=str%> // Now your dynamic array is ready on the client side
      for (i=0;i<=Content.length-1;i++)
            alert (Content[i]);


Author Comment

ID: 11714124
I'll try both of these ideas out this afternoon & get back to you.

Author Comment

ID: 11716206
Excellent answer by Zvonko - worked without any changes first time.  Thanks!

PradeepYadhav - your answer would have worked eventually I think, but needed some more work at my end & I don't have time to do it.  Sorry!

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question