2

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 come 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.

2 Answers 2

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, about the delay, does it have anything to do with document.ready?
Yes, document.ready is the same thing, it runs when the dom is parsed. you can use jQuery $(document).ready(myfunc); jQuery $(myfunc); or vanilla window.onload = myfunc;
How do i apply the script to where I want it in the html?
2

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.

4 Comments

You are a bit late... Did you use IE for this?
Yah I figured but there was an unanswered portion so I thought I would answer that. "Did you use IE for this"... Um what?
Yeah your answer is actually good but surely not useful anymore for who is asking. But keep it here because someone could need it later. IE = Internet Explorer.
Yah I know you mean Internet Explorer. What are you asking? Why? I'm on my Android phone stack overflow app.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.