Random image with javascript -


without knowing javascript @ somehow created random image generator:

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

i show random images should there limitations need help.

  1. sometimes have more 9 images pages , name "mad-01", "mad-02"... "mad-11" keep them sorted code above can't show images 0 before second digit. have named images , without 0 before second digit: "mad-01", "calm-1". how can solved? or better remove 0 images?
  2. is there way make code way won't need keep writing how many images there show?

i have code shortest possible that's why i'm not using array.

  1. i removing 0 file names easiest solution. depends on whether need or not.

  2. not sure mean here. javascript can't detect how many images there in file system on own. if you're using kind of server side language might have functionality counting files, , print value javascript code.


if want keep leading zeros in file names, this:

<script language="javascript"><!--  var number = math.floor(math.random()*12+1); if(number < 10) number = "0" + number; document.write("<img src=\"image-"+number+".jpg\" />"); //--></script> 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -