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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Location
    UK
    Posts
    207
    Thanks
    6
    Thanked 2 Times in 2 Posts

    Text linked to random image

    Hi

    I have this script that brings up a random jpeg image (in this case it is a red box, yellow box and blue box). Thats great.

    Thing is, I want it to have a line of text underneath it that says "Red", "Yellow" or "Blue"

    I'm sure this is easy to do, but I've had a few month away from web design and i'm forgetting everything i've learned.

    Thanks

    Code:
    <head>
    
    <title></title>
    
    </head>
    
    <body >
        
        <script>
        <!--
        var pic, alt;
        pic = new Array;
        pic[0] = "red.jpg";
        pic[1] = "yellow.jpg";
        pic[2] = "blue.jpg";
        
        alt = new Array;
        alt[0] = "This is a picture of red";
        alt[1] = "This is a picture of yellow";
        alt[2] = "This is a picture of blue";
    
        
        var now = new Date();
    	var seed = now.getTime() % 0xffffffff;
        
       function rand(n) {
    	seed = (0x015a4e35 * seed) % 0x7fffffff;
    	return ( seed >> 16 ) % n;
       }
    
    	var num2 = rand(3);
    
        document.write("<a href='" + pic[num2] + "' ><img alt='" + alt[num2] + "' name='main_image' src='" + pic[num2] + "' width='125' border='0' ></a>");
     
        //-->
        </script>
               
    </body>

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Give this a try

    PHP Code:
    <script language="text/javascript">
    <!--

    var 
    pic = [
    [
    "red.jpg","This is a picture of red","Red"],
    [
    "yellow.jpg","This is a picture of yellow","Yellow"],
    [
    "blue.jpg","This is a picture of blue","Blue"]
    ]


    var 
    now = new Date();
    var 
    seed now.getTime() % 0xffffffff;

    function 
    rand(n) {
    seed = (0x015a4e35 seed) % 0x7fffffff;
    return ( 
    seed >> 16 ) % n;
    }

    var 
    num2 rand(3);

    document.write("<a href='" pic[num2][0] + "' ><img alt='" pic[num2][1] + "' name='main_image' src='" pic[num2][0] + "' width='125' border='0' ></a><div>"+pic[num2][2]+"</div>");
     
    //-->
    </script> 
    Instead of using document.write() you could go

    PHP Code:
    <script type="text/javascript">
    <!--

    var 
    pic = [
    [
    "red.jpg","This is a picture of red","Red"],
    [
    "yellow.jpg","This is a picture of yellow","Yellow"],
    [
    "blue.jpg","This is a picture of blue","Blue"]
    ]


    function 
    rnd() {
    num=Math.floor(Math.random()*pic.length)
    document.getElementById("mydiv").innerHTML='<a href="'pic[num][0] +'"><img alt="'pic[num][1] +'" name="main_image" src="'pic[num][0] +'" width="125" border="0"></a><div>'+pic[num][2]+'</div>'
    }

    onload=rnd


     
    //-->
    </script>

    <div>
    <div id="mydiv"></div> 
    Last edited by Mr J; 10-28-2006 at 05:24 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

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