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 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2003
    Location
    SD, CaLi
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    random image script

    hi

    was just wondering if anyone knew a script for a random image generator..i cant seem to find one, and i dont know the script :x

    ex. i have 3 pictures
    when i go to my site... u'd see one of the pictures in the middle, but when u refresh/reload the page.. a different picture would be there.. and yea

    was just wondering if anyone knew a script for that, and if you could please show me if you did

    thx in advance
    €([-=JpDm=-])

  • #2
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script type="text/javascript">
    //<![CDATA[
    	onload = function()
    	{
    		var images =
    		[
    			'foo1.png',
    			'foo2.png',
    			'foo3.png'
    		];
    
    		document.images[0].src = images[Math.floor(Math.random()*images.length)];
    	}
    //]]>
    </script>
    Code:
    <img src="blank.png" alt="" />

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if u plan on calling ur pictures the same with a simple number increaser the array from above reply becomes redundant: u could then just write.
    And this is only my opinion but refering to a specific image in the documents image array by its number is something one should stay away from since it hampers development of at site, use id locating or some other method. That is only my opinion.


    onload = function()
    {
    var pics=3;
    document.getElementById('pic').src ="foo"+Math.floor(Math.random()*pics);
    }


    Image:
    <img src="blank.png" alt="" id="pic" />


    I got a quenstion I seen many use this <![CDATA[
    but what does it do?


  •  

    Posting Permissions

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