1

There are 0.png, 1.png, 2.png, ... in folder images. How to load all them. Number of the images unknown.

} while(!img[numOfImages].onerror);

alert("numOfImages = " + numOfImages);

var	
  numOfImages = 0;
  img = [];	

do{
	img[numOfImages] = new Image();
	img[numOfImages].src = "images/" + numOfImages + ".png";
	numOfImages++;
} while(!img[numOfImages].onerror);
alert("numOfImages= " + numOfImages);

1 Answer 1

1

Code:

    var dir = "Src/themes/base/images/";
var fileextension = ".png";
$.ajax({
    //This will retrieve the contents of the folder if the folder is configured as 'browsable'
    url: dir,
    success: function (data) {
        //List all .png file names in the page
        $(data).find("a:contains(" + fileextension + ")").each(function () {
            var filename = this.href.replace(window.location.host, "").replace("http://", "");
            $("body").append("<img src='" + dir + filename + "'>");
        });
    }
});

This will load all the image .png present in a folder, care that this code use jquery.

Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to load images without using Ajax?
You can't because basic javascript doesn't have directly access the contents of a file system.
You can't know the number witj javascript. Read here: stackoverflow.com/questions/1266004/… You can only with ajax: stackoverflow.com/questions/29232134/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.