[Java Script]Random Pics

Discussion in 'Web Design & Coding' started by luish, Feb 15, 2005.

  1. luish

    luish OSNN Senior Addict

    Messages:
    429
    Location:
    Israel
    Hey !
    I need a script to show random pic and i need a bit of help.
    I found a script, and I changed it a bit but i need to to random the names of the pics too. i will explain.
    I got pics in a directory let's say Images, that are numbered,like1205.jpg, 1206.jpg, etc... and I want the script to random me anumberfrom 2067-2089 (the name of the pics I want to random). and showthemrandomaly... but I got a problem doin it.
    This is the script i got... how can i upgrade it for my needs ?

    Code:
    <script language="JavaScript">
    <!--
    
    function random_images(){
      var myimages=new Array()
     
      myimages[1]="1.jpg"
      myimages[2]="2.jpg"
    
      var ry=Math.floor(Math.random()*myimages.length)
    
      if (ry==0)
    	 ry=1
    	 document.write('<img src="'+myimages[ry]+'" border=0>')
    }
    
    //-->
    </script>
    
    
    <SCRIPT language="JavaScript">
    <!--
    random_images()
    //-->
    </SCRIPT>
    
    Anyone ?
     
  2. j79zlr

    j79zlr Glaanies script monkey Political User

    Messages:
    2,725
    Location:
    Chicago
    This is all you would need, put this in your <head> section

    Code:
    <script type="text/javascript">
    <!--
    function random_images() {
      var num_of_pics;
      var starting_number;
      num_of_pics=22;
      starting_number=2067;
      rand=Math.random()*num_of_pics + starting_number;
      document.write("<img src=\"images/" + Math.round(rand) + ".jpg\" />");
    }
    //-->
    </script>
    The where you want the image:

    <script type="text/javascript">
    <!--
    random_images()
    //-->
    </script>
     
  3. luish

    luish OSNN Senior Addict

    Messages:
    429
    Location:
    Israel
    Thank you man !