Home > Web Front-end > JS Tutorial > body text

How to Load Images from a Folder into Your Web Page with jQuery/Javascript?

DDD
Release: 2024-11-04 06:55:30
Original
677 people have browsed it

How to Load Images from a Folder into Your Web Page with jQuery/Javascript?

Loading Images from a Folder into Web Page with jQuery/Javascript

To load images from a folder named "images" into an HTML page, you can use JavaScript or jQuery. Even if the image names are not sequential numbers, there's a solution.

Solution:

<code class="js">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 +"'>" );
            } 
        });
    }
});</code>
Copy after login

Explanation:

Considerations:

  • Apache Server: Enable "Option Indexes" or use a package like "serve-index" for Express.js to allow directory listings.
  • Node.js Server: Use the following code to enable folder listings:
<code class="js">app.use('/images', serveIndex(path.join(__dirname, '/images')));</code>
Copy after login

By using this approach, you can easily load all images from your "images" folder into your web page, regardless of their names.

The above is the detailed content of How to Load Images from a Folder into Your Web Page with jQuery/Javascript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template