...

View Full Version : Can't figure .src problem



kippo
07-07-2003, 07:43 AM
This function is called with Onload in the body tag of my code, this is so i can pass a vital bit of data to 'dir'.

'dir' is a number refering to a db id attribute, which matches up with a directory name where 8 images of that db item are stored.

Eveything works fine if i place a number where the ???'s are below, but obviouly this is no good as i get the same images for each db item. I need this ??? to be the value in 'dir'?

How do i get the directory at ??? to change to the vaule held in 'dir'?

function preload_img(dir)
{

pic1=new Image(400,400);
pic1.src="images/???/01.jpg";
pic2=new Image(400,400);
pic2.src="images/???/02.jpg";
pic3=new Image(400,400);
pic3.src="images/???/03.jpg";
pic4=new Image(400,400);
pic4.src="images/???/04.jpg";
pic5=new Image(400,400);
pic5.src="images/???/05.jpg";
pic6=new Image(400,400);
pic6.src="images/???/06.jpg";
pic7=new Image(400,400);
pic7.src="images/???/07.jpg";
pic8=new Image(400,400);
pic8.src="images/???/08.jpg";

}

really appreciate any advice! i just can't get anything to work, although i am quiet new to js.
Cheers

kwhubby
07-07-2003, 08:40 AM
well if u did something like this for each line u declare a pic variable


pic1.src="images/" + dir + "/01.jpg";


,,, one other thing... it would be lot easier than typing line after line for adding more pictures... and would be easier to acces later if u used an array
like this...

function preload_img(dir)
{
var x = 8
// u make that var x right there = how many pics u have
pic = new Array()
for (var i = 1; i <=x; i++) {
pic[i]=new Image(400,400);
pic[i].src="images/" + dir + "/01.jpg";
}
}

.. if u need to access one of those those pics later ... like before u might have had pic5 is now pic[5] ... and u can put a variable... in the array like pic[i]

beetle
07-07-2003, 08:43 AM
Hey kippo

What you need is called "string concatenation". Basically, cumulatively assembling a string from several pieces.

You first break from the string with a delimeter at the point that you need to insert the variable's data. Concat (short for 'concatenation') with the + operator, then go right back into the literal string.
function preload_img(dir)
{
pic1 = new Image(400,400);
pic1.src = "images/" + dir + "/01.jpg";
//Other images
} That clear enough?Of course, you could also do this
function preload_img(dir)
{
var root = "images/" + dir + "/";
pic1 = new Image(400,400);
pic1.src = root + "01.jpg";
//Other images
}

kwhubby
07-07-2003, 08:45 AM
lol... looks like beetle wrote and posted a reply at the same time... he posted just after i posted mine.

Garadon
07-07-2003, 11:12 AM
from kwhubby post.


function preload_img(dir)
{
var x = 8
// u make that var x right there = how many pics u have
pic = new Array()
for (var i = 1; i <=x; i++) {
pic[i]=new Image(400,400);
pic[i].src="images/" + dir + "/01.jpg";
}
}


it should be.



function preload_img(dir)
{
var x = 8
// u make that var x right there = how many pics u have
pic = new Array()
for (var i = 1; i <=x; i++) {
pic[i]=new Image(400,400);
pic[i].src="images/" + dir + "/0"+x+".jpg";
}
}
x

kippo
07-07-2003, 07:58 PM
Thanks everyone, the

pic1.src = "images/" + dir + "/01.jpg";

works a treat, i tried this already but couldn't get it to work, must have been my bad syntaxing!

I'm gonna try using the array methods though as its better coding practice.

Cheers again.

kwhubby
07-08-2003, 01:13 AM
oh yeah garadon... my error, i forgot about that... thanks for finding that, that will save kippo frusteration when mine doesnt work right :o :D

kwhubby
07-08-2003, 02:04 AM
woops actually garadon it should be
function preload_img(dir)
{
var x = 8
// u make that var x right there = how many pics u have
pic = new Array()
var j
for (var i = 1; i <=x; i++) {
(i<10)? j="0" + i: j=i;
pic[i]=new Image(400,400);
pic[i].src="images/" + dir + "/" + j + ".jpg";
}
}


not x... x is how many pics he wants to go through with that patern with... it should be i.... i is the variable that increments in the loop.... BUT>>>>> if he were to have more pics than 9 u woulnt want that 0 there so.... u make a variable j to have the 0 there if its less than 10

...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum