Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load all the images from one of my folder into my web page, using Jquery/Javascript

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?

like image 825
rishy Avatar asked Aug 28 '13 06:08

rishy


People also ask

How do I display all images in a directory in HTML?

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.

How do I put an image in the same folder as HTML file?

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 .

How do I add a photo to another folder from another website?

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.

How do you put images in a folder?

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.


1 Answers

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'))); 
like image 113
Roko C. Buljan Avatar answered Oct 14 '22 19:10

Roko C. Buljan