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

x
?
Solved

Dynamic array with Javascript

Posted on 2010-08-24
12
Medium Priority
?
943 Views
Last Modified: 2012-05-10
Classic ASP VBScript
IE7, Chrome, Firefox
WIN XP Pro

Hello, all

I am using a DHTML Fade Slideshow script from the net.
It works perfectly when static.

I altered it by adding a recordset and then a Repeat Region around the information string for the slideshow images, and entered dynamic data for the parameters.
= Worked fine... In Chrome and Firefox.

IE7 has a problem with the trailing comma.  Chrome and Firefox appear to not care about it.

So... my question is... how do I get the comma to NOT display on the last record?

Here is the snippet:
=======================================================
<%while NOT rsHomeimages.EOF%>
            ["<%=rsHomeimages("homeimageURL")%>","","",""],

<%
rsHomeimages.movenext()
wend
%>
=======================================================

You will notice the comma at the end of the parameter string because it is an array.
Again - Chrome and Firefox do not care that a trailing comma is at the end.
IE7 will not display the images unless the last record removes the comma.

I have tried <%if NOT rs.EOF then%>,<%end if%>, but.... same problem.
Surely there must be a way to only display the comma while it is not the last record.

Your help is most appreciated.

Thank you!
Shane
0
Comment
Question by:lshane
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 22

Expert Comment

by:Om Prakash
ID: 33518505
You can store this in variable and remove the comma from the end.
Example:

<%
dim myArr

while NOT rsHomeimages.EOF
        myArr = myArr & "[" & rsHomeimages("homeimageURL") & "],"
      rsHomeimages.movenext()
wend

'Once this is done you can remove the comma from the end.
myArr = left(myArr, len(myArr) - 1)
'and assign above variable to javascript array..
%>
0
 
LVL 2

Expert Comment

by:FEOX
ID: 33518543
Try this:


<%
  Dim l_Start as Boolean = True
  While NOT rsHomeimages.EOF %>
    If (NOT l_Start) Then
      %>,<%
    Endif
    %>
    ["<%=rsHomeimages("homeimageURL")%>","","",""]
    <%
    l_Start = False
    rsHomeimages.movenext()
  Wend
%>

Open in new window

0
 

Author Comment

by:lshane
ID: 33521352
Thank you, om_prakash_p and FEOX, but it still does not seem to be coming through.

om_prakash_p:
With your example, the page loads, but there is a small square in the center of where the images are to be.  The properties indicate it is the "loading" image, presumable from the slideshow script.  It does that for both Chrome and IE7.

FEOX:
Your example produced an IE7 error:
Error Type:
Microsoft VBScript compilation (0x800A0401)
Expected end of statement
/nfc/nfc8/default2.asp, line 51, column 12
Dim l_Start as Boolean = True
-----------^

Not sure what that means.

I will attach the entire script block (Not large), for maybe a little more context.
<script type="text/javascript">

var mygallery=new fadeSlideShow({  
	wrapperid: "fadeshow1", //ID of blank DIV on page to house Slideshow
	dimensions: [698, 237], //width/height of gallery in pixels. Should reflect dimensions of largest image
	imagearray: [




<%while NOT rsHomeimages.EOF%>
		["<%=rsHomeimages("homeimageURL")%>","","",""],

<%
rsHomeimages.movenext()
wend
%>



	],
	displaymode: {type:'auto', pause:5000, cycles:0, wraparound:false},
	persist: false, //remember last viewed slide and recall within same session?
	fadeduration: 1000, //transition duration (milliseconds)
	descreveal: "ondemand",
	togglerid: ""
})


</script>

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 29

Expert Comment

by:Badotz
ID: 33523761
Put the comma at the BEGINNING of the string, not the end. Then you can use Mid(string,2) to skip past it:

...
...
myArr = myArr & "," & rsHomeimages("homeimageURL")
...
...

myArr = "[" & Mid(myArr,2) & "]" ' Ts-da! No more bad comma!!
0
 

Author Comment

by:lshane
ID: 33523934
Hello, Badotz.

So, I think I placed your strings in the correct places, but it is still just placing a small square in the center of where the images are to be.

I'll attach the code.

What am I doing wrong?
<script type="text/javascript">

var mygallery=new fadeSlideShow({  
	wrapperid: "fadeshow1", //ID of blank DIV on page to house Slideshow
	dimensions: [698, 237], //width/height of gallery in pixels. Should reflect dimensions of largest image
	imagearray: [



<%
dim myArr

while NOT rsHomeimages.EOF
myArr = myArr & "," & rsHomeimages("homeimageURL")
      rsHomeimages.movenext()
wend

'Once this is done you can remove the comma from the end.
myArr = "[" & Mid(myArr,2) & "]" ' Ts-da! No more bad comma!!
'and assign above variable to javascript array..
%>



	],
	displaymode: {type:'auto', pause:5000, cycles:0, wraparound:false},
	persist: false, //remember last viewed slide and recall within same session?
	fadeduration: 1000, //transition duration (milliseconds)
	descreveal: "ondemand",
	togglerid: ""
})




</script>

Open in new window

0
 
LVL 29

Expert Comment

by:Badotz
ID: 33524067
If you don't want an array of arrays (which I don't think you want), try this:

myArr = Mid(myArr,2) ' Ta-da! No more bad comma!!
0
 
LVL 29

Expert Comment

by:Badotz
ID: 33524114
Also, I'm not sure you can place ASP within a script block? You might try stuffing the final value of "myArr" into a hidden tect field and referencing THAT from your javascript:

<input id="hdn_array" type="hidden" value="<%=myArr%>" />

and

imagearray: [ document.getElementById("hdn_array").value ];
0
 

Author Comment

by:lshane
ID: 33524651
Hmmm....

Well, ASP does appear to work fine in the original context:

<%while NOT rsHomeimages.EOF%>
            ["<%=rsHomeimages("homeimageURL")%>","","",""],

<%
rsHomeimages.movenext()
wend
%>


... but I just wanted the comma to not display on the "last record".

So, it's a repeating region, so it goes through the 4 records of the db just fine; however, I just need the comma at the end of it to NOT display on the final pass.

It works fine in Chrome and Firefox with the trailing comma... but IE wants the comma to be gone at the end of the repeat.

So the static rendering would be:

["pic1.jpg","","",""],
["pic2.jpg","","",""],
["pic3.jpg","","",""],
["pic4.jpg","","",""]

The last one should not have a comma.

I just can't figure out how to write the condition to only display the comma for all passes of the repeat region... except the final pass.

0
 
LVL 29

Expert Comment

by:Badotz
ID: 33524772
The problem with that code is that you send it to the browser directly, and you cannot "undo" what has been done. That is why you must process the data entirely BEFORE you submit it.
0
 
LVL 2

Expert Comment

by:FEOX
ID: 33526369
Try this instead:
<%
  Dim l_Start
  l_Start = True
  While NOT rsHomeimages.EOF %>
    If (NOT l_Start) Then
      %>,<%
    Endif
    %>
    ["<%=rsHomeimages("homeimageURL")%>","","",""]
    <%
    l_Start = False
    rsHomeimages.movenext()
  Wend
%>

Open in new window

0
 
LVL 29

Expert Comment

by:Badotz
ID: 33526513
Or this, perhaps?


<%
  Dim myArray
  Dim Q
  Q = chr(34) & chr(34 ' Double-quotes
  
  While NOT rsHomeimages.EOF %>
    myArray = myArray & ",[" & rsHomeimages("homeimageURL") & "," & Q & "," & Q & "," & Q & "]"
    
    rsHomeimages.movenext()
  Wend
  
  myArray = Mid(myArray, 2) ' Scrub leading comma
  
' Now your dynamic array "myArray" is ready to insert into the page
%>

<script type="text/javascript">

var mygallery=new fadeSlideShow({  
	wrapperid: "fadeshow1", //ID of blank DIV on page to house Slideshow
	dimensions: [698, 237], //width/height of gallery in pixels. Should reflect dimensions of largest image
	imagearray: [<%=myArray%>]
...
...
</script>

Open in new window

0
 

Accepted Solution

by:
lshane earned 0 total points
ID: 33528261
Well, I wasn't able to accomplish what I needed with the suggestions.

I was, however, able to get it working using ASP in the script block.

It all came down to creating 2 recordsets (1 for SELECT * for images, and the 2nd for a SELECT MAXid for the images).

I inserted and IF... THEN around the comma at the end of the string to only show the comma when current image ID being passed through was not equal to the MAXid (or MAXHOME, as I named it).  On the final repeat when the imageID became equal to the MAXHOME (MAXid), the comma was no longer added in.

= Came right up in IE and still in all the other browsers.

I attached the code, in case someone else finds it useful.

Thanks,
Shane
rsHomeimages ("SELECT * FROM homeimages ORDER BY homeimageID ASC")
...
...
rsMaxhomeimage ("SELECT MAX(homeimageID) AS maximage FROM homeimages")
...
...


<HTML>
<HEAD>

<%dim maxhome 'establishing a variable to more easily call it below
maxhome=rsMaxhomeimage("maximage")
%>

<script type="text/javascript">

var mygallery=new fadeSlideShow({  
	wrapperid: "fadeshow1", //ID of blank DIV on page to house Slideshow
	dimensions: [698, 237], //width/height of gallery in pixels. Should reflect dimensions of largest image
	imagearray: [


<%while NOT rsHomeimages.EOF%>

            ["<%=rsHomeimages("homeimageURL")%>","<%=rsHomeimages("homeimageLink")%>","<%=rsHomeimages("homeimageTarget")%>","<%=rsHomeimages("homeimageCaption")%>"]<%if rsHomeimages("homeimageID")<>maxhome then%>,<%end if%>

<%
rsHomeimages.movenext()
wend
%>



	],
	displaymode: {type:'auto', pause:5000, cycles:0, wraparound:false},
	persist: false, //remember last viewed slide and recall within same session?
	fadeduration: 1000, //transition duration (milliseconds)
	descreveal: "ondemand",
	togglerid: ""
})

</script>

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

772 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