Javascript: Random pix with link

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • antony
    New Member
    • Apr 2006
    • 2

    Javascript: Random pix with link

    Hi, everybody in forum.

    I got a problem to write a script. I appreciate to get your interests and helps.

    I got hundred of images (pix\img001.jpg .....img100.jpg ). I would like to create a page when opening the images (not one image but more than two images) in that folder will be shown randomly with the link to its folder.

    For example: To show 3 images within the page. Firstly RANDOM function will creat a number 12, then the image file will be shown should be img012.jpg, img013.jpg, and img013.jpg

    Please help me. Thank first for your help.
    Last edited by antony; Apr 22 '06, 04:15 PM. Reason: More explaination
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    To generate a random number between 1 and 100:
    [code=javascript]var rand = Math.floor(100 * Math.random()) + 1;[/code]
    For 1 to 200, this would be changed to:
    [code=javascript]var rand = Math.floor(200 * Math.random()) + 1;[/code]and so on.

    To generate the file names from the random numbers, you will need to pad numbers less than 100 with, e.g.
    [code=javascript]var pad = "";
    if (rand < 100) pad += "0";
    if (rand < 10) pad += "0";
    filename1 = "pix/img" + pad + rand + ".jpg";
    filename2 = "pix/img" + pad + (rand+1) + ".jpg";
    filename3 = "pix/img" + pad + (rand+2) + ".jpg";[/code]You'll need to include a check for the last image and perhaps roll round to the first image.

    The final step of setting the src and linking the image is trivial.

    Comment

    Working...