Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 488
  • Last Modified:

Creating a Javascript array from a VBScript Array

Hi

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>^"
Next
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()
content[0]="<%=arrText(0)%>"
content[1]="<%=arrText(1)%>"
content[2]="<%=arrText(2)%>"

function regenerate()
{
      window.location.reload()
}

function regenerate2()
{
      if (document.layers)
      {
            appear()
            setTimeout("window.onresize=regenerate",450)
      }
}

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>')
            document.d1.document.d2.document.close()
      }
}

function appear()
{
      document.d1.visibility='show'
}

window.onload=regenerate2
//========================================
//========================================

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 http://www.sabretele.com 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?
0
s9
Asked:
s9
  • 2
1 Solution
 
ZvonkoSystems architectCommented:
Like this:


<script>
var content= [
<%  For a = 0 to UBound(arrText) %>
   "<%=arrText(a)%>" ,
<%  Next %>
   ""];
content.length--;
</script>

0
 
PradeepYadhavCommented:
Hi Buddy

    Hope this helps you.

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

</script>
0
 
s9Author Commented:
I'll try both of these ideas out this afternoon & get back to you.
0
 
s9Author Commented:
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!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now