Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-28-2006, 10:40 AM   PM User | #1
many_tentacles
Regular Coder

 
Join Date: Dec 2005
Location: UK
Posts: 207
Thanks: 6
Thanked 2 Times in 2 Posts
many_tentacles is an unknown quantity at this point
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>
many_tentacles is offline   Reply With Quote
Old 10-28-2006, 05:08 PM   PM User | #2
Mr J
Senior Coder

 
Join Date: Aug 2002
Location: UK
Posts: 2,789
Thanks: 2
Thanked 14 Times in 14 Posts
Mr J is on a distinguished road
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> 
__________________
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.

Last edited by Mr J; 10-28-2006 at 05:24 PM..
Mr J is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:44 AM.


Advertisement
Log in to turn off these ads.