mercredi 27 janvier 2016

Random image with javascript

Without knowing javascript at all I somehow created random image generator:

<script language="JavaScript"><!-- 
document.write("<img src=\"image-"+Math.floor(Math.random()*12+1)+".jpg\" />");
//--></script>

I does show random images as it should but there are some limitations so I need your help.

  1. Sometimes I have more than 9 images for some pages and their name is "mad-01", "mad-02"... "mad-11" to keep them sorted but code above can't show images with 0 before second digit. I have named images with and without 0 before second digit: "mad-01", "calm-1". How can this be solved? Or is it better to just remove 0 from images?
  2. Is there a way to make this code that way so I won't need to keep writing how many images is there to show?

I would like to have this code as shortest as possible that's why I'm not using array.




Aucun commentaire:

Enregistrer un commentaire