View Full Version : Attaching URL's to random images using Javascript?
boebarl1
April 23rd, 2010, 05:20 PM
I am working on this webpage and I have written the script so that a random image pops up every time you refresh the page.
Now what I am trying to do is connect a url to each image. I want the same url for each image every time. I know I have been trying different things but am not sure if I am going about this the right way. If someone could take a look at this and give me some suggestions I would really appreciate it. You can check it out at
http://boebarlage.limewebs.com/
any feed back will be greatly appreciated.
Thanks alot!
boebarl1
skullsnest
April 23rd, 2010, 10:16 PM
If your adamant about doing it this way, rewrite the script tag like this.
var links = ["http://www.google.com", "http://www.kirupa.com", "http://www.webdeveloper.com", "http://www.yahoo.com", "http://www.youtube.com",];
//pick a random image from the pictures array and displays by creating an img tag and
//appending the src attribute to the filename
var randNum = Math.floor( Math.random() * 5 );
document.write("<a href='" + links[randNum] + "'><img src = 'images/" +
pictures[randNum] + ".jpg' /></a>");
</script>
replacing my links for yours. also, I would put the script inside the body tag, as it is writing to the body.
hope this helps
skullsnest
April 23rd, 2010, 10:18 PM
Sorry, missed some of the code
<script type = "text/javascript">
var pictures = ["beanbag", "golf", "softball", "volleyball", "walk"];
var links = ["http://www.google.com", "http://www.kirupa.com", "http://www.webdeveloper.com", "http://www.yahoo.com", "http://www.youtube.com",];
//pick a random image from the pictures array and displays by creating an img tag and
//appending the src attribute to the filename
var randNum = Math.floor( Math.random() * 5 );
document.write("<a href='" + links[randNum] + "'><img src = 'images/" +
pictures[randNum] + ".jpg' /></a>");
</script>
boebarl1
April 24th, 2010, 11:38 AM
Thanks for the tip!
I knew that I was going to have to make 2 arrays, but I was unsure how to connect them so that the same url would stay with the same image each time. Anyway, your code works great and is simple to understand.
Thanks again,
boebarl1
skullsnest
April 24th, 2010, 01:31 PM
no problem, your welcome
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.