?
Solved

Random rolling images with image specific links

Posted on 2005-03-25
5
Medium Priority
?
189 Views
Last Modified: 2008-02-26
Hi all,

Easiest way to describe this is to think of it as displaying ads on a page.

I need to be able to randomly display 2 images with thier specific links (the links will always be the same for any given image.
At the moment I have:

<script language="JavaScript">
var theImages1 = new Array() // do not change this
theImages1[0] = 'http://www.mydomain.com/common/images/rand01.gif'
theImages1[1] = 'http://www.mydomain.com/common/images/rand02.gif'
var j = 0
var p = theImages1.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages1[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage1(){
document.write('<img src="'+theImages1[whichImage]+'">');
}
</script>

Which is ok but doesn't do any linking and in order to make sure the same image is not displayed twice I have to do two lots of the above with independant 'image lists' e.g.

<script language="JavaScript">
var theImages1 = new Array() // do not change this
theImages1[0] = 'http://www.mydomain.com/common/images/rand01.gif'
theImages1[1] = 'http://www.mydomain.com/common/images/rand02.gif'
var j = 0
var p = theImages1.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages1[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage1(){
document.write('<img src="'+theImages1[whichImage]+'">');
}
</script>
<script language="JavaScript">
var theImages2 = new Array() // do not change this
theImages2[0] = 'http://www.mydomain.com/common/images/rand03.gif'
theImages2[1] = 'http://www.mydomain.com/common/images/rand04.gif'
var j = 0
var p = theImages2.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages2[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage2(){
document.write('<img src="'+theImages2[whichImage]+'">');
}
</script>

An all in one solution is desired.. cheers in advance.
0
Comment
Question by:phreak7t7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13630414
<script language="JavaScript">
var theImages = new Array() // do not change this
var theLinks = new Array() // do not change this
theImages[0] = 'http://www.mydomain.com/common/images/rand01.gif'
theImages[1] = 'http://www.mydomain.com/common/images/rand02.gif'
theImages[2] = 'http://www.mydomain.com/common/images/rand03.gif'
theImages[3] = 'http://www.mydomain.com/common/images/rand04.gif'
theLinks[0] = 'http://www.mydomain.com/common/images/rand01.html'
theLinks[1] = 'http://www.mydomain.com/common/images/rand02.html'
theLinks[2] = 'http://www.mydomain.com/common/images/rand03.html'
theLinks[3] = 'http://www.mydomain.com/common/images/rand04.html'
var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var whichImage1 = Math.round(Math.random()*(p-1));
var whichImage2 = whichImage1;
while (whichImage2 == whichImage1) {
  whichImage2 = Math.round(Math.random()*(p-1));
}
document.write('<a href="'+theLinks[whichImage1]+'"><img src="'+theImages[whichImage1]+'"></a>');
document.write('<a href="'+theLinks[whichImage2]+'"><img src="'+theImages[whichImage2]+'"></a>');

</script>
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13630434
and you can remove the
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}

or move it after the calculation so you can just prebuffer the chosen images
0
 

Author Comment

by:phreak7t7
ID: 13630629
The bonus question :o) : is it possible to make the images with links randomly change in page say every 5 seconds as an example as apposed to just when the page loads?
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 750 total points
ID: 13630695
Yes


<script language="JavaScript">
var theImages = new Array() // do not change this
var theLinks = new Array() // do not change this
theImages[0] = 'http://www.mydomain.com/common/images/rand01.gif'
theImages[1] = 'http://www.mydomain.com/common/images/rand02.gif'
theImages[2] = 'http://www.mydomain.com/common/images/rand03.gif'
theImages[3] = 'http://www.mydomain.com/common/images/rand04.gif'
theLinks[0] = 'http://www.mydomain.com/common/images/rand01.html'
theLinks[1] = 'http://www.mydomain.com/common/images/rand02.html'
theLinks[2] = 'http://www.mydomain.com/common/images/rand03.html'
theLinks[3] = 'http://www.mydomain.com/common/images/rand04.html'
var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
function changeImage() {
  var whichImage1 = Math.round(Math.random()*(p-1));
  var whichImage2 = whichImage1;
  while (whichImage2 == whichImage1) {
    whichImage2 = Math.round(Math.random()*(p-1));
  }
  document.getElementById('imageDiv1').innerHTML='<a href="'+theLinks[whichImage1]+'"><img src="'+theImages[whichImage1]+'"></a>';
  document.getElementById('imageDiv2').innerHTML='<a href="'+theLinks[whichImage2]+'"><img src="'+theImages[whichImage2]+'"></a>';
}

</script>
</head>
<body onLoad="changeImage(); setInterval('changeImage()',5000)">
<div id="imageDiv1"></div><div id="imageDiv2"></div>
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13630930
Any reason for the "B" grade???
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question