/    Sign up×
Community /Pin to ProfileBookmark

Random Image Rotating with corresponding Hyperlinks

Hello. What I am wanting to do is have random images rotate on my index page. I’m wanting these images to be hyperlinks to the corresponding products page. I added the link array at top for reference on the 2nd script below but how do I change the function at the bottom to have the corresponding links with the images matched up together? Or how do I modify the first script to incorporate the image rotation/cycle without the refresh?
————————

Some more info. The first example displays a random picture with the correct link associated with the picture. The only problem is the script doesn’t automatically reload another random image without a refresh. If you hit refresh and reload the page you will get a different random image with a link.

The second example randomly changes the images on the page every 6 seconds or so. The only thing about this one is I’m not sure how to associate this function with the links and make it work. I’ve tried a few things but can’t get it to work.

What I’m wanting is a combination of the two. I want a random picture with its corresponding link when clicked. If this image isn’t clicked I want it to change or cycle from one of the random linked pictures to another.
Thanks for your help!!

[CODE]
<!–I have this which isn’t rotating pictures but the link works:–>
<script language=”JavaScript”>

function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]=”../new/images/cc/random_images/4719lr.jpg”
myimages[2]=”../new/images/cc/random_images/6754lr.jpg”
myimages[3]=”../new/images/cc/random_images/71HHGlr.jpg”

//specify corresponding links below
var imagelinks=new Array()
imagelinks[1]=”http://www.link1.com”
imagelinks[2]=”http://www.link2.com”
imagelinks[3]=”http://www.link3.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>’)
}
//–>
</script>

<!–
##############################
Second try below
##############################

Or I have this which rotates images but I’m not sure how to add the link var to the function at bottom:
–>

<script language=”JavaScript”>
<!– Begin

var imagelinks=new Array()
imagelinks[1]=”http://www.link1.com”
imagelinks[2]=”http://www.link2.com”
imagelinks[3]=”http://www.link3.com”

var interval = 2.5; // delay between rotating images (in seconds)
var random_display = 1; // 0 = no, 1 = yes
interval *= 1500;

var image_index = 0;
image_list = new Array();
image_list[image_index++] = new imageItem(“../new/images/cc/random_images/4719lr.jpg”);
image_list[image_index++] = new imageItem(“../new/images/cc/random_images/6754lr.jpg”);
image_list[image_index++] = new imageItem(“../new/images/cc/random_images/71HHGlr.jpg”);

var number_of_image = image_list.length;
function imageItem(image_location) {
this.image_item = new Image();
this.image_item.src = image_location;
}
function get_ImageItemLocation(imageObj) {
return(imageObj.image_item.src)
}
function generate(x, y) {
var range = y – x + 1;
return Math.floor(Math.random() * range) + x;
}
function getNextImage() {
if (random_display) {
image_index = generate(0, number_of_image-1);
}
else {
image_index = (image_index+1) % number_of_image;
}
var new_image = get_ImageItemLocation(image_list[image_index]);
return(new_image);
}
function rotateImage(place) {
var new_image = getNextImage();
document[place].src = new_image;
var recur_call = “rotateImage(‘”+place+”‘)”;
setTimeout(recur_call, interval);
}
// End –>
</script>
[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@PadonakOct 22.2008 — try this

<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;script language="JavaScript" type="text/javascript"&gt;

/*here we create an array of img/links*/

var img_links = new Array(
{pic:"../new/images/cc/random_images/4719lr.jpg", lnk:"http://www.link1.com"},
{pic:"../new/images/cc/random_images/6754lr.jpg", lnk:"http://www.link2.com"},
{pic:"../new/images/cc/random_images/71HHGlr.jpg", lnk:"http://www.link3.com"}
)

/*let's go :-))*/

function randomImg(){
var now = new Date();
var num = (now.getSeconds())%eval(img_links.length);
var place = document.getElementById("place");
place.innerHTML = "&lt;a href='" + img_links[num]["lnk"] + "'&gt;&lt;img src='" + img_links[num]["pic"] + "'&gt;&lt;/a&gt;";
// the 2 lines below are just for demonstration
// and can be easily removed
place.innerHTML += "&lt;br&gt;&lt;br&gt;src = " + img_links[num]["pic"];
place.innerHTML += "&lt;br&gt;&lt;br&gt;href = " + img_links[num]["lnk"];
setTimeout("randomImg()", 2000);
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="randomImg()"&gt;
&lt;div id="place"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@Guest85authorOct 22.2008 — This one works better than the other I have because it's still random. The only problems I see is the images are surrounded by a purple or blue border because they are hyperlinks I'm assuming. Also, below the image, text for the image source and link path are displayed. Any fixes for these 2 thing?

Thanks Padonak!
Copy linkTweet thisAlerts:
@PadonakOct 23.2008 — friend if you take a look at the code i gave you will see 2 commented lines which describe everything about "text for the image source and link path are displayed". it is made just for demonstration and can be easily removed from the script or just commented to be disabled. please do not be that leech and do that little thing by yourself ok? :-)

as for colored borders - your question shows that you do not know much about styles or CSS (cascading style sheets). i would recommend you to read something about this because your sites without using this technology are going to be so to say "not modern" :-) also i would recommend you to learn much about hypertext markup language aka "HTML" and JavaScript of course.

to remove the colored borders replace the line

<img src='" + img_links[num]["pic"] + "'>

with this one

<img src='" + img_links[num]["pic"] + "' border="0">

or this one

<img src='" + img_links[num]["pic"] + "' style="border:none">

or use this function which makes every image on your page to have no any border

function noBorders(){

var dim = document.images;

for(var i = 0; i < dim.length; i++){

dim[i].style.border = "none";

}

}



best regards & greetings from russia
×

Success!

Help @Guest85 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...