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 09-18-2012, 12:02 AM   PM User | #1
vitriol
New to the CF scene

 
Join Date: Sep 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
vitriol is an unknown quantity at this point
Help for code

Hello,
I have this code:
.......................

Code:
<script language="javascript">
var delay=1000 //set delay in miliseconds
var curindex=0

var randomimages=new Array()

	randomimages[0]="1.jpg"
	randomimages[1]="5.jpg"
	randomimages[2]="2.jpg"
	randomimages[3]="4.jpg"
	randomimages[4]="3.jpg"
	randomimages[5]="6.jpg"

var preload=new Array()

for (n=0;n<randomimages.length;n++)
{
	preload[n]=new Image()
	preload[n].src=randomimages[n]
}

document.write('<img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*(randomimages.length))]+'">')

function rotateimage()
{

if (curindex==(tempindex=Math.floor(Math.random()*(randomimages.length)))){
curindex=curindex==0? 1 : curindex-1
}
else
curindex=tempindex

	document.images.defaultimage.src=randomimages[curindex]
}

setInterval("rotateimage()",delay)

</script>
....................................
I would like to add url link to each image.
How can i do that?
Many thank to help me.
vitriol is offline   Reply With Quote
Old 09-18-2012, 03:15 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
Then you should change the document.write line to include an <a> anchor tag warpped around the <img> tag

Something in the lines of
Code:
document.write('<a href="whatever.html"><img name="defaultimage" src="'+randomimages[Math.floor(Math.random()*(randomimages.length))]+'"></a>');
devnull69 is offline   Reply With Quote
Old 09-18-2012, 04:15 PM   PM User | #3
vitriol
New to the CF scene

 
Join Date: Sep 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
vitriol is an unknown quantity at this point
Thanks devnull69 but i want a different url for each iumage...so how can i do that.
Many thanks in advance.
vitriol is offline   Reply With Quote
Old 09-18-2012, 05:27 PM   PM User | #4
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
You did a randomimages array; make a randomURL array, and use it to populate the href of the anchor.
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 09-18-2012, 05:30 PM   PM User | #5
vitriol
New to the CF scene

 
Join Date: Sep 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
vitriol is an unknown quantity at this point
Thanks WolfShade but how do you do that...i have very limited knowledge of javascript so please help me.

Could you post the code.
many thanks.
vitriol is offline   Reply With Quote
Old 09-18-2012, 08:20 PM   PM User | #6
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
Code:
var randomURL=new Array()

	randomURL[0]="whatever1.html"
	randomURL[1]="http://www.whatelse.com/whatever2.html"
	randomURL[2]="whatever3.php"
	randomURL[3]="something.html"
	randomURL[4]="where/what.html"
	randomURL[5]="whatever4.html"

...


var idx = Math.floor(Math.random()*(randomimages.length));
document.write('<a href="' + randomURL[idx] + '"><img name="defaultimage" src="'+randomimages[idx]+'"></a>');
devnull69 is offline   Reply With Quote
Old 09-18-2012, 11:28 PM   PM User | #7
vitriol
New to the CF scene

 
Join Date: Sep 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
vitriol is an unknown quantity at this point
many thank
vitriol 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 03:13 PM.


Advertisement
Log in to turn off these ads.