Geeth Welagedara Geeth Welagedara - 1 month ago 11
Javascript Question

How to load all the images from one Directory

i tried to display all images in one directory using following

jquery
. But it is not working. My folder structure is just a images folder and
js
folder.
I just followed this question also, but couldnt achive the target.

enter image description here



< script >
$(document).ready(function() {

var folder = "images/";
$.ajax({
url: folder,
success: function(data) {
$(data).find("a").attr("href", function(i, val) {
if (val.match(/\.(jpe?g|png|gif)$/)) {
$("body").append("<img src='" + folder + val + "'>");
}
});
}
});
}); < /script>

<!DOCTYPE html>
<html>

<head>
<script src="js/jquery.min.js"></script>

</head>

<body>
</body>

</html>




Answer

First of all you need to create a server page which will provide you list of names from that directory. you need to call that page (instead of folder name) from your $.ajax function.

Second, loop over that list of image names (paths) and create image elements. You are doing the similar stuff.