admin管理员组

文章数量:1323157

Hey I am trying to figure out a way for me to add some media(images and videos) to my webpage. I have a section where I add only images and videos, and it is HTML-based So I thought I could find a way to just create one folder for each section, add all the media in that folder, and then let Javascript do the work. This is the script I have e up with so far.

Javascript

var files = {'jpg':4};
var pageName = "Wimoveh";
for (var ext in files){
for (var i = 0; i < files[ext]; i++){
    var src = "Images/GalleryOne" + pageName + "-" + (i+1) + "." + ext;}
}

This script looks in the folder GalleryOne for the picture with name Wimoveh with a '-' as separator and then iterates through all the content that matches that name and extension. What I dont know is how to output the result in my HTML.

Hey I am trying to figure out a way for me to add some media(images and videos) to my webpage. I have a section where I add only images and videos, and it is HTML-based So I thought I could find a way to just create one folder for each section, add all the media in that folder, and then let Javascript do the work. This is the script I have e up with so far.

Javascript

var files = {'jpg':4};
var pageName = "Wimoveh";
for (var ext in files){
for (var i = 0; i < files[ext]; i++){
    var src = "Images/GalleryOne" + pageName + "-" + (i+1) + "." + ext;}
}

This script looks in the folder GalleryOne for the picture with name Wimoveh with a '-' as separator and then iterates through all the content that matches that name and extension. What I dont know is how to output the result in my HTML.

Share asked Oct 14, 2012 at 16:39 DruckerDrucker 271 gold badge2 silver badges5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

Inside your loop:

var img = new Image(); 
img.src = src;
containerElement.appendChild(img);

Where containerElement is a reference to the html element you want to add the images to. (note that you'll need to delay until the dom has loaded to do so)

As Generalhenry stated in his post:

Where containerElement is a reference to the html element you want to add the images to. (note that you'll need to delay until the dom has loaded to do so)

The containerElement is what ever HTML element you want it to be. For example:

<HTML>
<BODY>
    <div class="photos">
        Your images would populate here.
    </div>
</BODY>
</HTML>

Every time loop through your script when the code reaches photos.appendChild(IMG) your page will add an image to that div class.

本文标签: iterating through image folder using javascript and adding the result in HTMLStack Overflow