Solved

Javascript Random Image Selector

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

772 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