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 11 of 11
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    multiple random image scripts on one page - possible??

    using this script http://www.javascriptkit.com/script/cut118.shtml
    can i have more than one in different locations on the same page? nothing i've tried works but i'm no javascript expert....

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Untested but it should work.

    Code:
    <script language="JavaScript">
    <!--
    
    /*
    Random Image Script- By JavaScript Kit (http://www.javascriptkit.com) 
    Over 400+ free JavaScripts here!
    Keep this notice intact please
    */
    
    function random_imglink(){
    var myimages=new Array()
    //specify random images below. You can have as many as you wish
    myimages[1]="image1.gif"
    myimages[2]="image2.gif"
    myimages[3]="image3.gif"
    myimages[4]="image4.gif"
    myimages[5]="image5.gif"
    myimages[6]="image6.gif"
    
    for (i = 0; i < 3; i++) {
         var ry=Math.floor(Math.random()*myimages.length)
         if (ry==0)
         ry=1
         document.write('<img src="'+myimages[ry]+'" border=0>')
    }
    }
    random_imglink()
    //-->
    </script>
    If you want the images to be in other locations that all together the script could be modified so it assigns images with a certain id a random image. So you could have the images scattered on the page and then just running the function would assign them random images.
    OracleGuy

  • #3
    New Coder
    Join Date
    Jul 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ok, talk to me like i'm four. i'm not getting that to work.
    do i list all the random images in one place? say i want 5 images shown at once -
    how do i tell it to pick from random1.jpg to random10.jpg for the first random image,
    11-20 for the second, 21-30 for the third etc.?

    what i tried before i posted was using 5 copies of the script, adding a number to 'myimages' and 'random_imglink'
    so i had 'myimages1', 'myimages2', 'random_imglink1', 'random_imglink2' etc. it would load random images in the 5 places TOO randomly!
    sometimes 3 would show up with the other two showing the broken image thing, sometimes only one would show,
    sometimes 2, and about every 50th reload all five would show up just fine.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    try this:
    Code:
    <script language="javascript">
    <!--
    var gallery = new Array();
    var gallery[0] = new Array("image1.gif","image2.gif","image3.gif");
    var gallery[1] = new Array("image4.gif","image5.gif","image6.gif");
    var gallery[2] = new Array("image7.gif","image8.gif","image9.gif");
    
    function pickImageFrom(whichGallery){
      var idx = Math.floor(Math.random()*gallery[whichGallery].length);
      document.write('<img src="'+gallery[whichGallery][idx]+'" border=0>');
    }
    //-->
    </script>
    ...
    <body>
    ...
    <script language="javascript">pickImageFrom(0);</script>
    ...
    <script language="javascript">pickImageFrom(1);</script>
    ...
    <script language="javascript">pickImageFrom(2);</script>
    ...
    </body>

  • #5
    New Coder
    Join Date
    Jul 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i can't get it to work with that code either. no images show up, not even the broken image thing.

  • #6
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    I think it is time for you to give us a link to the page where it isn't working.

    It will be a lot easier to determine what is breaking it.
    OracleGuy

  • #7
    New Coder
    Join Date
    Jul 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here it is: http://www.how2llc.com/test/random.html

    for anyone wanting to know how to get it to work, this works perfectly:

    <script language="javascript">
    <!--
    var gallery = new Array();
    gallery[0] = new Array("01.jpg","02.jpg","03.jpg","04.jpg","05.jpg");
    gallery[1] = new Array("06.jpg","07.jpg","08.jpg","09.jpg","10.jpg");
    gallery[2] = new Array("11.jpg","12.jpg","13.jpg","14.jpg","15.jpg");
    gallery[3] = new Array("16.jpg","17.jpg","18.jpg","19.jpg","20.jpg");
    gallery[4] = new Array("21.jpg","22.jpg","23.jpg","24.jpg","25.jpg");

    function pickImageFrom(whichGallery)
    {
    var idx = Math.floor(Math.random() * gallery[whichGallery].length);
    document.write('<img src="images/random/' + gallery[whichGallery][idx] + '">');
    }
    //-->
    </script>
    </head>
    <body>

    <div align="center"><script language="javascript">pickImageFrom(0);</script><br /><br />
    <script language="javascript">pickImageFrom(1);</script><br /><br />
    <script language="javascript">pickImageFrom(2);</script><br /><br />
    <script language="javascript">pickImageFrom(3);</script><br /><br />
    <script language="javascript">pickImageFrom(4);</script>
    </div>

    thanks for the help!
    Last edited by badmix; 10-22-2003 at 12:52 AM.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    it works for me..random images showed up from corresponding galleries. What browser are you using?

  • #9
    New Coder
    Join Date
    Jul 2002
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah, the last code i posted is working perfectly. thanks for checking it!!

  • #10
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how can I add a 'specific' link to each image?
    Last edited by wissenwill; 01-28-2009 at 05:43 PM.

  • #11
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    Found a solution with links, but...

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 TRANSITIONAL//EN">
    
    <html>
    <head>
    	<script type="text/javascript">
    	var gallery = new Array();
    	gallery[0] = new Array(new Array ("module_01.jpg", "#01"), new Array ("module_02.jpg", "#02"), new Array ("module_03.jpg", "#03"), new Array ("module_04.jpg", "#04"));
    	gallery[1] = new Array(new Array ("module_05.jpg", "#05"), new Array ("module_06.jpg", "#06"), new Array ("module_07.jpg", "#07"), new Array ("module_08.jpg", "#08"));
    
    
    	function pickImageFrom(whichGallery)
    	{
    	var idx = Math.floor(Math.random() * gallery[whichGallery].length);
    	
    	document.write('<a href="' + gallery[whichGallery][idx][1] + '"><img src="images/' + gallery[whichGallery][idx][0] + '"></a>');
    	}
    	</script>
    </head>
    <body>
    
    <script language="javascript">pickImageFrom(0);</script><br /><br />
    <script language="javascript">pickImageFrom(1);</script><br /><br />
    </body>
    </html>
    ...it looks crapy... How can I do this better? Some small help from a professional would be great! THX!!


  •  

    Posting Permissions

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