Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Javascript loop

Posted on 2004-11-23
3
Medium Priority
?
179 Views
Last Modified: 2010-04-17
I'm trying to preload a variable number of images. I can do most of what I want in asp but I'm not good with js.  I'd like to use only js in the load command but will consider using both vbscript and js. The snippet below is what i want but it's not rendering.

****************CODE**************

'Determine the number of images
picnumof = ((Recordset1.Fields.Item("Pictures - Number of").Value) -1)
'DEBUG TO BE REMOVED
'response.write picnumof


<%
for i=1 to picnumof
imageload = imageload + "'http://vxxxxxxxxx.maximumasp.com/ress/img/40047429_0" & i &".jpg', "
next
%>

<body onLoad="MM_preloadImages('<%=imageload%>')">
0
Comment
Question by:webdork
3 Comments
 
LVL 3

Expert Comment

by:weddell
ID: 12660069
Using a mix of client-side javascript and server-side vbScript might be easier.


<%
'Determine the number of images
picnumof = ((Recordset1.Fields.Item("Pictures - Number of").Value) -1)
'DEBUG TO BE REMOVED
'response.write picnumof
%>

<script language="JavaScript"><!--

    for(var i=0; i < <%=picnumof%>; i++) {
           imageLoad = imageLoad + '/ress/img/40047429_0' + i + '.jpg';
    }

//--></script>

<body onload="MM_preloadImages(imageLoad)">
0
 
LVL 11

Accepted Solution

by:
pratap_r earned 2000 total points
ID: 12662814
nu uh.. it wont work webdell, because of 2 reasons, first is you are not seperating the image files with a delimiter so you get a parameter like

/ress/img/40047429_00.jpg/ress/img/40047429_01.jpg/ress/img/40047429_02.jpg and so on

the second being MM_preloadimages (assuming the author is using the one generated by macromedia suite of apps) takes an array of parameters and not as a single string of concatenated values..

Now for the solution. webdork, your code doesn't work because of a two mistakes. First, If you notice the preparation of parameters that you are doing, you are appending a , at the end of each file. This , will be present at the last file also. Meaning, your java function call will look like

MM_preloadImages('/ress/img/40047429_01.jpg','/ress/img/40047429_02.jpg',);

this is what is creating the problem for you. so you need to strip of the last , and a space after that

do this by using
if picnumof > 0 then imageload=Left(imageload, Len(imageload) - 2)

that will strip off your final , and a space

The second mistake being,
<body onLoad="MM_preloadImages('<%=imageload%>')">

notice that you are using '<% for a javascript call so your actual rendering will look like

MM_preloadImages(''image1.jpg','image2.jpg'')
that's not a double quote but rather two ', that is the problem. just do away with the single quote.. your line should be

<body onLoad="MM_preloadImages(<%=imageload%>)">

your modified code ...

****************CODE**************

'Determine the number of images
picnumof = ((Recordset1.Fields.Item("Pictures - Number of").Value) -1)
'DEBUG TO BE REMOVED
'response.write picnumof


<%
for i=1 to picnumof
imageload = imageload + "'http://vxxxxxxxxx.maximumasp.com/ress/img/40047429_0" & i &".jpg', "
next
if picnumof > 0 then imageload=Left(imageload, Len(imageload) - 2)
%>

<body onLoad="MM_preloadImages(<%=imageload%>)">

Have Fun!
Pratap
0
 

Author Comment

by:webdork
ID: 12668534
Hi Thanks all for your contribution. I'm out of town for Tgiving and away from the PC ball and chain. Back to work on Mon and will test latest suggestions and respond then. (No time for good grammer).
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
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…
Starting up a Project

577 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