Solved

Javascript Random Image Selector

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

746 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

11 Experts available now in Live!

Get 1:1 Help Now