views:

158

answers:

6

I am trying to rewrite my code below to search a folder for all the images (they will be numbered but there maybe gaps, ie not 1.jpg,2.jpg,3.jpg but instead 1.jpg,15.jpg,60.jpg for this reason i would like to search the folder, put all the images into an array and then pick one randomly each time its looped.

Any help would be greatly appreciated.

Firstly i am currently specifying image total above the main script below:

imgWidth = 160,
imgHeight = 95,
imgTotal = 22,
total = 0,
tiles;


//create the HTML for the tiles and append that to the bg element
 function makeTiles(count){
     var html = '', imgNum;
     while(count--){
  imgNum = Math.floor(Math.random()*imgTotal + 1);
  html += "<div class='tile' style='background:url(public/images/portfolio/all/"+imgNum+".jpg) 0 0 no-repeat;' ><img style='opacity:0; filter:alpha(opacity=0);' src='public/images/portfolio/all/"+imgNum+"-c.jpg' alt='' /></div>\r";
     }
     $bg.append(html);
 }
+2  A: 

javascript can not access local folders. point.

I repeat: there is no way you can "search folder" to get "array of images" in JS. You could do that part (server only!) in PHP or such server-side language and return results via AJAX.

dusoft
ah, is there any workaround?
Andy
Not local folders, folders on the server...
Andy
if the URL is on the server (not locally), you can use PHP to generetae array and pass it to JS via AJAX.
dusoft
JSON format will be probably best suited for this.
dusoft
i didnt downvote anyone, i dont even know what that is
Andy
A: 

To do what you want you need to know what the images are called. JavaScript cannot access folder directly as commented above. You would need to use a server side script to provide an array of the images for the JS to pick at random to do this.

matpol
+2  A: 

You'll need to create a list of available images with something else than javascript, since it has no filesystem access, even though in the end, you are accessing the images via their url.

Workaround: enable some directory listing for the images, then access this page via javascript, parse the image files and construct an array out of them; but frankly, there are shorter and more robust ways to accomplish this ...

pseudocode ..

$ ls -1 *jpg > imagesfilelist.txt
$ cp imagefilelist.txt /some/publicly/accessible/folder

js/jquery ..

$.get("/some/publicly/accessible/folder/imagefilelist.txt", function(data){
   alert("My image files: " + data);
});

...

The MYYN
haha, that is very hackish workaround, but clever indeed!
dusoft
in the imagefilelist.txt then could i just print an array of images?
Andy
i posted pseudocode. if you go this way, maybe you want to prepare your imagelist in json format, which could be natively parsed into an e.g. javascript array ..
The MYYN
A: 

Javascript will not be able to browse folders. What you need to do is to create an array of available images and then select a random one. You could do this using any server side technology (php, rails, java, .net ...).

marcgg
The trouble is this entire script is in a javascript file so i would have to include the PHP elsewhere
Andy
You can make an asynchronous call to the PHP, calling a "give me a random image name" function
marcgg
How do you do that please? Sorry.
Andy
Lookup the ajax functions of jquery http://docs.jquery.com/Ajax there's a lot of documentation out there
marcgg
A: 

The way you're trying to do it is a wrong one.But iwth a bit of tricks it could work though, but it's very wrong way to do this kind of things.

You can generate file list with php and feed it to your script. You can even create php script which will generate your script already populated with needed data but it's not the best to do this too.

So, the best ways are: - create html with list of filenames/images(visible or invisible) by php, then manipulate it by javascript; - create html and javascript wich will do AJAX query to php script which will return filename list(formated as JSON if you wish).

stroncium
Also if you needn't manipulate your images on the page you needn't even to use javascript, you can just generate html with php(or other server-side language).
stroncium
Yes, ive only shown you a small snippet to keep it simple...its doing a great deal more than above.
Andy
AJAX query to JSON-generating php script is the best option for you then. (If you will come up with script which gives listing of directory passed from client it can be a security hole if you will not properly sanitize input.)
stroncium
A: 

Why not upload your images to a free hosting site (like Flickr) grab the feed from your image group and select the random image from there?

fudgey