How to load all the images from one folder into my web page using Jquery
Initialize n.
your current function
var dir = "img/";
var fileextension = ".jpg";
var i = 1;
var n;
$(function imageloop(){
$("<img />").attr('src', dir + i + fileextension ).appendTo("#images");
for(i=1;i<n;i++){
imageloop();
};
});
here value of i
will never change since it is always calling itself.so it will append 1.jpg
till the time stack limit is over.
change your code to.
var dir = "img/"; var fileextension = ".jpg"; $(document).ready(function(){ var i = 1; var n = 3; for(i=1;i<n;i++){ imageloop(i); }; }); function imageloop(i){ $("<img />").attr('src', dir + i + fileextension ).appendTo("#images"); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div id = "images"> </div>
How to load all the images from one Directory
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.
How can I auto load multiple images from a local folder using javascript/jquery?
I don't clearly know but I don't think you can actually read files using javascript . At least on the client side using raw javascript . You can however upload some files and then store it inside that specific app you are building
The main reason you can't do it in Javascript from the client side is because it would be risky for normal users , any javascript developers could hack into user's file system via creating a client side javascript exploit
If you want to upload files using raw vanilla javascript , you can do it like this ...
<input id="image-file" type="file" />
Then handle it via javascript like this
document.getElementById("image-file").addEventListener('onclick', function(e){
const file = e.target.files[0];
// your code
})
but if you really want to load images from a specific directory , you have to use a server side language like Node.js , you can learn to do it like this
Reading Images to a webpage from folder Javascript/Jquery
Something like this?
var folder = "folder/path/";
$.ajax({ url: folder, success: function(data) { console.log("success"); $(data).find("a").attr("href", function(i, val) { if (val.match(/\.(jpe?g|JPE?G|png|PNG|gif|GIF)$/)) {
var output = "<img src='" + val + "'>"; $("#YourContainer").append(output); } });
}, error: function(e) { console.log(e + " error"); }});
Get all images from local folder
I think your best option is to use the new File API in Javascript. Is has a lot of functions to read files from the file system.
<input type="file" id="fileinput" multiple />
<script type="text/javascript">
function readMultipleFiles(evt) {
//Retrieve all the files from the FileList object
var files = evt.target.files;
if (files) {
for (var i=0, f; f=files[i]; i++) {
var r = new FileReader();
r.onload = (function(f) {
return function(e) {
var contents = e.target.result;
alert( "Got the file.n"
+"name: " + f.name + "n"
+"type: " + f.type + "n"
+"size: " + f.size + " bytesn"
+ "starts with: " + contents.substr(1, contents.indexOf("n"))
);
};
})(f);
r.readAsText(f);
}
} else {
alert("Failed to load files");
}
}
document.getElementById('fileinput').addEventListener('change', readMultipleFiles, false);
</script>
(code from here)
You can find a good explanation and helpful code here.
Related Topics
React: Do Children Always Rerender When the Parent Component Rerenders
Wait for Asynchronous Functions to Finish in Angular
How to Get Full Path of Selected File on Change of <Input Type='File'> Using Javascript, Jquery-Ajax
How to Pass Model Data in JavaScript
Trigger Event When Element Becomes Visible With Ngif
How to Share and Edit Global Variables Between Two JavaScript Node Runs
How to Pass Id from Modal in Boostrap to PHP File
How to Run HTML File on Localhost
Convert Image from Url to Base64
How to Remove Errors Warnings from Console
Regular Expression to Get a String Between Two Strings in JavaScript
Vue.Js Add Objects to Existing Array
How to Hide Select Options With Javascript (Cross Browser)
Display Images in React Using Jsx Without Import
Multiple Key Names, Same Pair Value
Customizing Item-Text in V-Select