Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript Random Image Selector

Posted on 2011-03-04
5
Medium Priority
?
563 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

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.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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…

722 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