Forum Moderators: open

Message Too Old, No Replies

Need a Random Linked Image JavaScript

I have a script, but I don't know how to edit it.

         

soulkissed

11:31 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



Hello all, I'm hoping you can help me. I need a Random Linked Image JavaScript which will:

1. Show a random image (which links to an external web page) each time the page is reloaded/refreshed.

2. Contain TITLE, WIDTH, HEIGHT, ALT information in the rendered link, like so:

<a href="http://www.webpage.com/" title="LINK DESCRIPTION" rel="external"><img src="image.jpg" width="50" height="50" alt="ALT INFORMATION" /></a>

3. Open in a new window when random linked image is clicked, but I don't want to use TARGET tags (which wont validate in XHTML Strict). I already have a JavaScript which will do this (see: rel="external" in above example). Can they be used together?

4. Be in a separate file, so I can include it anywhere I want on a page like so:

<script type="text/javascript" src="../scripts/random.js"></script>

5. Be editable, so I can add more linked images if need be.

I've found a script which shows random linked images, but it doesn't include all of the info I want. Can anyone show me how to edit it, please? I'd really appreciate it! Here's the script:

<script language="JavaScript">
<!--

/*
Random Image Link Script- By JavaScript Kit(http://www.javascriptkit.com)
Over 200+ free JavaScripts here!
Updated: 00/04/25
*/

function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="image1.gif"
myimages[2]="image2.gif"
myimages[3]="image3.gif"
myimages[4]="image4.gif"
myimages[5]="image5.gif"
myimages[6]="image6.gif"

//specify corresponding links below
var imagelinks=new Array()
imagelinks[1]="http://www.javascriptkit.com"
imagelinks[2]="http://www.netscape.com"
imagelinks[3]="http://www.microsoft.com"
imagelinks[4]="http://www.dynamicdrive.com"
imagelinks[5]="http://www.freewarejava.com"
imagelinks[6]="http://www.cnn.com"

var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<a href='+'"'+imagelinks[ry]+'"'+'><img src="'+myimages[ry]+'" border=0></a>')
}
random_imglink()
//-->
</script>

soulkissed

11:54 pm on Feb 9, 2006 (gmt 0)

10+ Year Member



I've already figured out how to do Step 4: Just put everything between the script tags and comment tags into a file named "random.js" and put it into the "scripts" subdirectory on my server, then link to it using the code I provided above.

I don't know how to write JavaScript code AT ALL, but I've managed to figure out from the script provided, I'd have to create more arrays for each random linked image which include the TITLE and ALT information.

I'm just not sure how to edit the way it renders on the page so that it's all together like I want, including the image size (which will be the same for all images).