I have a folder named "images" in the same directory as my .js file. I want to load all the images from "images" folder into my html page using Jquery/Javascript.
Since, names of images are not some successive integers, how am I supposed to load these images?
Simply run the command from a command line window in the directory where your images are stored. If you need to have the all. html in some other place either move it there or change to >> C:\files\html\all.
To link to a target file in the same directory as the invoking HTML file, just use the filename, e.g. my-image. jpg . To reference a file in a subdirectory, write the directory name in front of the path, plus a forward slash, e.g. subdirectory/my-image. jpg .
The <img> HTML tag It has 2 important attributes: src - Specifies the source location where the browser will look for the image file. Once it gets the image file, it loads the image on the web page. We can use a URL (Uniform Resource Locator) to display an image from another website.
Click the first picture you want to drag, hold shift, and then click on the last picture you want to drag. Now, you can easily drag the pictures into the folder.
Works both localhost and on live server without issues, and allows you to extend the delimited list of allowed file-extensions:
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 +"'>" ); } }); } });
NOTICE
Apache server has Option Indexes
turned on by default - if you use another server like i.e. Express for Node you could use this NPM package for the above to work: https://github.com/expressjs/serve-index
If the files you want to get listed are in /images
than inside your server.js you could add something like:
const express = require('express'); const app = express(); const path = require('path'); // Allow assets directory listings const serveIndex = require('serve-index'); app.use('/images', serveIndex(path.join(__dirname, '/images')));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With