mercredi 29 avril 2015

javascript random images script

I have an online store and I would like to make some improvements. I want to create a script which inserts me on every product page some random images of another products. But these images have to be linked with the description of that product and with the price. So far I got this :

function random_imglink(){
  var myimages=new Array()
  myimages[1]="imagepath1"
  myimages[2]="imagepath2"
  myimages[3]="imagepath3"
  ..............
  ..............

  var imagelinks=new Array()
  imagelinks[1]="link1"
  imagelinks[2]="link2"
  imagelinks[3]="link3"
  ................
  ................
  
  var Quotation=new Array()
  Quotation[1]="<h1>image 1</h1>"
  Quotation[2]="<h1>image 2</h1>"
  Quotation[3]="<h1>image 3</h1>"
  .............
  .............

  var price=new Array()
  price[1]="price1"
  price[2]="price2"
  price[3]="price3"
  ............
  .............
  
  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> '+Quotation[ry]+' '+price[ry]+'</a>')
     
}   

  random_imglink()

The problem is that I want more than just one image which gets randomising. I want 4 images to do that at every refresh... For example,if I create a table with one row and 4 columns and I copy the code above in every cell the script is working well. But I'm sure there is a beautiful solution.

I hope you people was able to understand me.Sorry for my english !




Aucun commentaire:

Enregistrer un commentaire