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 10 of 10
  1. #1
    New Coder
    Join Date
    Dec 2002
    Location
    Washington State
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Excluding an image from a random list

    Here is my Code:


    <script language="JavaScript" type="text/javascript">
    <!--
    n=3;// number of all gifs
    g=1;//number of random gifs

    //no need to modify bellow
    var theImages = new Array() //array of gifs
    for(var i=0;i<n;i++){
    theImages[i]='0'+i+'.gif';
    }
    //randomize function
    function rand(){
    r = Math.floor(Math.random()*n);
    return r;
    }
    function showImages(){
    var rr = new Array()//array of radom numbers
    rr[0]=-1;
    for(var j=0;j<g;j++){
    rand();
    for(var k=0;k<rr.length;k++){//check whether new random number reapets a former one or not
    if(k!=j&&r==rr[k]){
    var j=j-1;break
    }
    else{rr[j]=r;}//if not, create new array's element
    }
    document.getElementById('g'+j).setAttribute('src',theImages[rr[j]]);//set new image
    }
    }
    // -->
    </script>


    <a href="#" onclick="showImages();return false">
    <IMG SRC="00.gif" ID="g0" ALT="Click to View Your Fortune" BORDER="0"></A>





    What I am trying to figure out is how to EXCLUDE the origenal image (00.gif) from being one of the random selections. When the main image is clicked I want it to show a picture from a random list. However I don't want the main image to be in that list. Any comments/suggestions are greatly apreciated. Thanks.

    ~Isaac Lloyd~
    ~ Isaac Lloyd ~
    Website Design & Development
    http://www.IsaacLloyd.com

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    I think you may:

    - either build your first array excluding the 0
    - or remove the desired element from the final array using splice() method
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Dec 2002
    Location
    Washington State
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    could you give me an example or tell me how I could go about excluding the 0 on the first array, or how to do the splice() thing? Thanks for your time.


    ~Isaac Lloyd~
    ~ Isaac Lloyd ~
    Website Design & Development
    http://www.IsaacLloyd.com

  • #4
    New Coder
    Join Date
    Mar 2005
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Been a little while since I've done Javascipt, but I think this might do it for you.

    for(var i=0; i<n; i++)
    {
    theImages[i]='0' + (i + 1) +'.gif';
    }

  • #5
    New Coder
    Join Date
    Dec 2002
    Location
    Washington State
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    instead it showed a "no value" picture. It's like that comment makes that first image an error if it shows up again. I just don't want it to be included or show up again period. Thanks for your ideas though... Anyone else?

    ~Isaac Lloyd~
    ~ Isaac Lloyd ~
    Website Design & Development
    http://www.IsaacLloyd.com

  • #6
    New Coder
    Join Date
    Mar 2005
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry...I just don't remember enough about Javascript to give you hand.

  • #7
    New Coder
    Join Date
    Dec 2002
    Location
    Washington State
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How about you Mr. J. ? Any ideas?
    ~ Isaac Lloyd ~
    Website Design & Development
    http://www.IsaacLloyd.com

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Try this

    ...
    }
    if(theImages[rr[j]=='00.gif'){
    theImages[rr[j]=theImages[rr[j+1]];
    j++
    }
    document.getElementById('g'+j).setAttribute('src',theImages[rr[j]]);//set new image
    }
    }
    // -->
    </script>
    Last edited by Kor; 03-24-2005 at 09:10 AM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #9
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    or:
    if(theImages[rr[j]=='00.gif'){
    showImages()
    }
    else{
    document.getElementById('g'+j).setAttribute('src',theImages[rr[j]]);//set new image
    }
    }
    }
    // -->
    </script>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #10
    New Coder
    Join Date
    Dec 2002
    Location
    Washington State
    Posts
    98
    Thanks
    0
    Thanked 0 Times in 0 Posts
    *EDIT* Never mind. I realized afterwards that the image will always be 00.gif cause we never changed it yet. The two methods you suggested didn't work. Any ideas? Thanks for your time.



    not working for me... What about something like:


    ....

    IF (FORM.INPUT.SRC = "00.gif") { <----- needs real info. That doesn't work.
    showImages()
    }
    ELSE{
    document.getElementById('g'+j).setAttribute('src',theImages[rr[j]]);//set new image
    }



    My Image is: <INPUT TYPE="image" NAME="luckyimage" ID="g0" SRC="00.gif" onclick="checkluckyimg();return false" VALUE="newluck">

    Thanks for your time.


    ~Isaac Lloyd~
    Last edited by isaaclloyd; 03-24-2005 at 09:31 AM.
    ~ Isaac Lloyd ~
    Website Design & Development
    http://www.IsaacLloyd.com


  •  

    Posting Permissions

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