It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

Random image script

Random image script

Postby foreigner on Mon Mar 02, 2009 11:29 am

hi all :)

i want to create a random card picker and this script works great for me. but how can i add clickable links to cards after they are generated randomly? i want the new image generated in the same place where 0.jpg is when button is clicked exactly like in the script, but with a clickable link on it. i couldnt manage to add links to the images.

thanks in advance :)

-----------------------------------
<script language="javascript">

<!-- Begin

function show(){
var theImages = new Array()
theImages[0] = 'deck/card1.jpg'
theImages[1] = 'deck/card2.jpg'
theImages[2] = 'deck/card3.jpg'
theImages[3] = 'deck/card4.jpg'
theImages[4] = 'deck/card5.jpg'
theImages[5] = 'deck/card6.jpg'

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 whichImage = Math.round(Math.random()*(p-1));
document.picture.src = theImages[whichImage];
}// End -->

</script>

<img src="deck/0.jpg" name="picture" />

<form>
<input type="button" value="pick a card" onclick="show()">
</form>
foreigner
 
Posts: 2
Joined: Mon Mar 02, 2009 11:26 am

Re: Random image script

Postby rangana on Wed Mar 04, 2009 5:44 am

Code: Select all
<script type="text/javascript">
/**
Script Title: Random Image with Link [configurable via array]
Script Author: Raymond Angana as first seen on devppl.com/forum [username = rangana]
Date Created: 3/4/09
This notice must stay intact for legal use
*/
var ray = {
   imgs:
   [
      ['deck/card1.jpg','http://www.google.com'],
      ['deck/card2.jpg','http://www.yahoo.com'],
      ['deck/card3.jpg','http://www.card3.com'],
      ['deck/card4.jpg','http://www.msn.com'],
      ['deck/card5.jpg','http://www.card5.com'],
      ['deck/card6.jpg','http://www.cnn.com']
   ],
   
   getID:function(el) {
      return document.getElementById(el);
   },
   
   randomPic:function (el) {
      var aTag = this.getID(el);
      var rand = Math.floor(Math.random()*this.imgs.length);
      var imgSrc = aTag.innerHTML.split('src="')[1].split('"')[0];
      aTag.href = this.imgs[rand][1];
      aTag.getElementsByTagName('img')[0].src = this.imgs[rand][0];
   }
}
</script>

<a id="picturelink" href="#"><img src="deck/0.jpg" name="picture" /></a>

<input type="button" value="pick a card" onclick="ray.randomPic('picturelink')">
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Re: Random image script

Postby foreigner on Wed Mar 04, 2009 6:57 am

this is just what i needed :) Thank you very much for your kind help :)
foreigner
 
Posts: 2
Joined: Mon Mar 02, 2009 11:26 am


Who is online

Users browsing this forum: No registered users and 4 guests