...

View Full Version : Text linked to random image



many_tentacles
10-28-2006, 10:40 AM
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




<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>

Mr J
10-28-2006, 05:08 PM
Give this a try


<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


<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum