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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jun 2003
    Location
    uk
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Can't figure .src problem

    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

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    Carmel California
    Posts
    471
    Thanks
    0
    Thanked 1 Time in 1 Post
    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]
    Last edited by kwhubby; 07-07-2003 at 08:43 AM.
    Kris Hubby
    kwhubby site

  • #3
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    Code:
    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
    Code:
    function preload_img(dir) 
    {
        var root = "images/" + dir + "/";
        pic1 = new Image(400,400);
        pic1.src = root + "01.jpg";
        //Other images
    }
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Location
    Carmel California
    Posts
    471
    Thanks
    0
    Thanked 1 Time in 1 Post
    lol... looks like beetle wrote and posted a reply at the same time... he posted just after i posted mine.
    Kris Hubby
    kwhubby site

  • #5
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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

  • #6
    New to the CF scene
    Join Date
    Jun 2003
    Location
    uk
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    sorted

    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.

  • #7
    Regular Coder
    Join Date
    Nov 2002
    Location
    Carmel California
    Posts
    471
    Thanks
    0
    Thanked 1 Time in 1 Post
    oh yeah garadon... my error, i forgot about that... thanks for finding that, that will save kippo frusteration when mine doesnt work right
    Kris Hubby
    kwhubby site

  • #8
    Regular Coder
    Join Date
    Nov 2002
    Location
    Carmel California
    Posts
    471
    Thanks
    0
    Thanked 1 Time in 1 Post
    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

    ...
    Last edited by kwhubby; 07-08-2003 at 02:10 AM.
    Kris Hubby
    kwhubby site


  •  

    Posting Permissions

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