Solved

Dynamic array with Javascript

Posted on 2010-08-24
12
935 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 29

Expert Comment

by:Badotz
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 29

Expert Comment

by:Badotz
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now