Solved

Javascript Random Image Selector

Posted on 2011-03-04
5
536 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now