473,320 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Javascript: Random pix with link

2
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.
Apr 22 '06 #1
1 1635
acoder
16,027 Expert Mod 8TB
To generate a random number between 1 and 100:
Expand|Select|Wrap|Line Numbers
  1. var rand = Math.floor(100 * Math.random()) + 1;
For 1 to 200, this would be changed to:
Expand|Select|Wrap|Line Numbers
  1. var rand = Math.floor(200 * Math.random()) + 1;
and so on.

To generate the file names from the random numbers, you will need to pad numbers less than 100 with, e.g.
Expand|Select|Wrap|Line Numbers
  1. var pad = "";
  2. if (rand < 100) pad += "0";
  3. if (rand < 10) pad += "0";
  4. filename1 = "pix/img" + pad + rand + ".jpg";
  5. filename2 = "pix/img" + pad + (rand+1) + ".jpg";
  6. filename3 = "pix/img" + pad + (rand+2) + ".jpg";
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.
Apr 25 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Vladimir Veytsel | last post by:
Please find at the link below samples of JavaScript programming techniques along with several useful fully-functional JavaScripts. http://www.davar.net/INTERNET/JAVA-SCR/JAVA-SCR.HTM ...
2
by: sgMuser | last post by:
Hi, I am not a good developer of Javascript codes. Needs this help to make some modification to this famous free javascript from Anarchos. i am using this in one of my webpage. What it does is,...
4
by: bboyle18 | last post by:
Hi, I am working with a table sorting script which can be found here http://www.workingwith.me.uk/articles/scripting/standardista_table_sorting This script works very nicely, but when there is a...
9
by: mshepard | last post by:
I am very new to Javascript and would like some advise for the following... a. Declare a variable named rNumber = to the value returned from the randInt() function using 5 as the parameter value. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.