Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question loading images with a for loop

    I want to load a number of sequentially named images to display as a slidehow. Rather than typing in the names of the files, I want to use a for loop to cache the images. The coding I've come up with goes something like this:

    for (c=0;c<=20;c+1) {
    var imagec=newImage()
    imagec.scr="s'+c+'.gif"

    The idea is to use the count variable to create the variable image with a number suffix (ie image0, image1, image2...) and cache the file with the same value (ie. s0.gif, s1.gif, ...)

    How can I write this so that the image variable will increment and load the proper file?

    Ron

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    This line

    for (c=0;c<=20;c+1) {

    should be

    for (c=0;c<=20;c++) {
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have tried it both ways. I just need the syntax so that the 'c' in the variable imagec is the counter variable.

    Ron

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Just create an array instead of creating 20 variables.
    Code:
    var imgs = new Array(20);
    for (var c=0;c<imgs.length;c++) {
      imgs[c] = new Image();
      imgs[c].src = "s" + c + ".gif";
    }
    Then you access each image as imgs[0], imgs[1], ...imgs[19]
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New Coder
    Join Date
    Jan 2005
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Glenn, that seems to work. I'm just trying to write images to the page and the
    Code:
    document.write(imgs[c])
    doeant seem to work.

  • #6
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    Try it like this:

    Code:
    <html>
    	<head>
    		<script type="text/javascript">
    			function loadImg()
    			{
    				var imgs = new Array(20);
    				for (var c=0;c<imgs.length;c++)
    				{
    					imgs[c] = new Image();
    					imgs[c].src = "s" + c + ".gif";
    					var trgt=document.getElementById('imgspot');
    					trgt.appendChild(imgs[c]);
    				}
    			}
    		</script>
    	</head>
    	<body onload="loadImg()">
    		<div id="imgspot">
    		</div>
    	</body>
    </html>
    Basscyst
    Last edited by Basscyst; 01-15-2005 at 04:28 PM.
    Helping to build a bigger box. - Adam Matthews

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by rgn1703
    Thanks Glenn, that seems to work. I'm just trying to write images to the page and the
    Code:
    document.write(imgs[c])
    doeant seem to work.
    Code:
    document.write(imgs[c].src)
    That will write the image source only and not the whole image tag. You have to also write the tags if you want to display the images or use what Basscyst suggested.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •