Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript Random Image Selector

Posted on 2011-03-04
5
Medium Priority
?
575 Views
Last Modified: 2012-05-11
Hey all,

Here is the problem. I do not know Javascript very well. Happy with every other language :)

I need a simple script which will look at a shared folder, see X amount of images and randomly pick one.

The names will most likely not be in any format. I can easily find scripts like the one below but these require fixed image names.



<script language="JavaScript">
<!--

function random_imglink(){
  var myimages=new Array()
  //specify random images below. You can have as many as you wish
  myimages[1]="img1.gif"
  myimages[2]="img2.gif"
  myimages[3]="img3.gif"

  var ry=Math.floor(Math.random()*myimages.length)

  if (ry==0)
     ry=1
     document.write('<img src="'+myimages[ry]+'" border=0>')
}

  random_imglink()
//-->
</script>

Open in new window

0
Comment
Question by:KazooSoft
  • 3
  • 2
5 Comments
 
LVL 19

Expert Comment

by:Bardobrave
ID: 35035438
Mmmm.... if you haven't formatted names you will need to get the name of any selected file.

To do this you can use FileSystemObject, maybe return the list of images in the folder, obtain a random number, get the file with this number order and get it's name to build the <a> tag.

Check this link for info in using FileSystemObject with javascript:
http://www.java2s.com/Tutorial/JavaScript/0600__MS-JScript/0220__FileSystemObject.htm
0
 
LVL 4

Author Comment

by:KazooSoft
ID: 35035739
Hmm,

Im not sure what to call. But for example if I have 5 images in a path I know, I will need to somehow get javascript to find out these file names when select 1 at random.
0
 
LVL 4

Assisted Solution

by:KazooSoft
KazooSoft earned 0 total points
ID: 35035869
It looks like im going to have to use php.. Something im more comfortable with:

<?
$dir = "pics"; // The relative path to the image directory
$pictures = glob("$dir/{*.jpg,*.jpeg,*.gif,*.png}",GLOB_BRACE);
$img = $pictures[mt_rand(0,count($pictures)-1)];
echo '<img src="' . $img . '">';
?>
0
 
LVL 19

Accepted Solution

by:
Bardobrave earned 2000 total points
ID: 35037322
FileSystemObject will allow you to get files and folders and treat them as objects inside your code. You'll also able to retrieve it's filenames, extensions, etc.

Just set the FileSystemObject and pass the path of the folder to the GetFolder method, you'll get a collection of files inside the folder. Within that collection you can loop, get single file objects and access it's properties.

I'm not used to work with php so cannot put here a code snippet (not a reliable one at least), but it's very easy and intuitive to use.
0
 
LVL 4

Author Closing Comment

by:KazooSoft
ID: 35081178
I found the PHP option easier to use
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question