www.webdeveloper.com
Results 1 to 3 of 3

Thread: Image refresh Code! Need Expert help!

  1. #1
    Join Date
    Nov 2010
    Posts
    1

    Image refresh Code! Need Expert help!

    Hey, im designing a website and i need an image to change everytime it refreshes! I have a code and i am able to get picture one and two, but the 3 others never appear and leaves a blank where the picture should be!
    If anyone has to time to quickly read my code and tell me what i am doing wrong, i would greatly appreciate! Thank you

    <head>
    <SCRIPT LANGUAGE="JavaScript">
    var theImages = new Array()

    //Random-loading images
    theImages[0] = 'img/1.jpg'
    theImages[1] = 'img/2.jpg'
    theImages[2] = 'img/3.jpg'
    theImages[3] = 'img/4.jpg'
    theImages[4] = 'img/5.jpg'

    var j = 0
    var p = theImages.length;
    var preBuffer = new Array()

    for (i = 0; i < p; i++){
    preBuffer[i] = new Image()
    preBuffer[i].src = theImages[i]
    }
    var whichImage = Math.round(Math.random()*(p-1));

    function showImage(){
    if(whichImage==0){
    document.write('<a href ="link.html"><img src="'+theImages[whichImage]+'" border=0 width=790 height=491></a>');
    }
    else if(whichImage==1){
    document.write('<a href ="link.html"><img src="'+theImages[whichImage]+'" border=0 width=790 height=491></a>');
    }
    else if(whichImage==2){
    document.write('<a href ="link.html"><img src="'+theImages[whichImage]+'" border=0 width=790 height=491></a>');
    }
    else if(whichImage==3){
    document.write('<a href ="link.html"><img src="'+theImages[whichImage]+'" border=0 width=790 height=491></a>');
    }
    else if(whichImage==4){
    document.write('<a href ="link.html"><img src="'+theImages[whichImage]+'" border=0 width=790 height=491></a>');
    }

    }

    </script>


    </head>


    <body>

    <script>showImage();</script>
    </body>

    Thanks for your time!

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Works correctly. Perhaps the image names are incorrect or not available.
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Nov 2010
    Posts
    3
    Just tested it and indeed, it is correct, it probably can't find the images.

    Also, just a note, all your code in the showImage function

    Code:
    function showImage(){
    if(whichImage==0){
    document.write('<a href ="link.html"><img src="'+theImages[whichImage]+'" border=0 width=790 height=491></a>');
    }
    else if(whichImage==1){
    document.write('<a href ="link.html"><img src="'+theImages[whichImage]+'" border=0 width=790 height=491></a>');
    }
    else if(whichImage==2){
    document.write('<a href ="link.html"><img src="'+theImages[whichImage]+'" border=0 width=790 height=491></a>');
    }
    else if(whichImage==3){
    document.write('<a href ="link.html"><img src="'+theImages[whichImage]+'" border=0 width=790 height=491></a>');
    }
    else if(whichImage==4){
    document.write('<a href ="link.html"><img src="'+theImages[whichImage]+'" border=0 width=790 height=491></a>');
    }
    can be replaced by this:

    Code:
    function showImage(){
    document.write('<a href ="link.html"><img src="'+theImages[whichImage]+'" border=0 width=790 height=491></a>');
    }
    since it does the same for evey case.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles