Results 1 to 2 of 2

Thread: Random picture selection with links to different pages

  1. #1
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Random picture selection with links to different pages

    I want to try and create a script that will:
    a) Randomly insert a different .gif file
    b) With a link to another page specific to that .gif image.
    c) The .gif and link will change everytime the webpage is loaded/refreshed.

    I am thinking probably javascript is the easiest way to do this by using the VAR in Java, but I have no idea how to actually achieve this. Please can you suggest something.

    Thanks

    Yabzab

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <a id="tst" href="#"><img src="http://www.vicsjavascripts.org.uk/StdImages/Blank.gif" width="100" height="100" /></a>
    
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function Swap(id,ary){
     var a=document.getElementById(id);
     ary=ary[Math.floor(Math.random()*ary.length)];
     a.href=ary[1];
     a.getElementsByTagName('IMG')[0].src=ary[0];
    }
    
    var Ary=[
     ['http://www.vicsjavascripts.org.uk/StdImages/One.gif','http://www.vicsjavascripts.org.uk/'],
     ['http://www.vicsjavascripts.org.uk/StdImages/Two.gif','http://www.dynamicdrive.com/'],
     ['http://www.vicsjavascripts.org.uk/StdImages/Three.gif','http://www.codingforums.com/']
    ];
    
    Swap('tst',Ary);
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Bookmarks

Posting Permissions

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