2

I'm pretty new to jQuery and I am trying to achieve the following solution.

I am using a nice image carousel/slider that requires me to have a div on the page containing the images that I want the carousel to use. I want the flexibility to put images in a folder instead of hard coding them, and the images then be displayed, so:

  • a folder (img) on my server containing any number of images
  • some solution to create a list (array) of the images (their paths)
  • the list or array to be used with the carousel, whether it just generates the HTML or not I guess is half the problem!

I appreciate the help!

@RobW - as I said I am learning..

/--- edit: I found a solution using jQuery templates. It may not be the smartest, but here it is:

I created a div that I want the images placed in:

<div id="imageContainer"></div>     
<!-- Create template -->
    <script id="imageTemplate" type="text/x-jquery-tmpl">
        <img id="imageItem" src='../PublishingImages/${Name}' alt="${Desc}" />
    </script>
<!-- Get data -->
    <script type="text/javascript">
        $(document).ready(function () {
            $.getJSON(
                '../_vti_bin/ListData.svc/Images',
                    function(data) {
                        $('#imageTemplate')             //select the template
                            .tmpl(data.d.results)       //bind it to the data
                                .appendTo('#imageContainer');   //render the output
                                    });
                                    }); 
    </script>
2
  • I'm afraid this is not possible without using a server-side processing first like asp.net or php to populate the list of images. Commented Dec 12, 2011 at 10:46
  • So, what's the question? So far, your question looks like a set of requirements, without any code. Show what you have tried, and ask more specific questions. If you have no code, go hire a programmer. Commented Dec 12, 2011 at 10:47

3 Answers 3

1

You need PHP (or something) for this.

Have a look at the dynamic examples. There you can learn how to load the images. You will also need to write a small PHP script. Get startet with PHP. I guess you need mainly scandir and a foreach loop.

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

Comments

1

jQuery is a framework for Javascript which is a client-side language, so it is impossible for it to directly interact with your server to get a file list from a folder.

To achieve this you'd need to use a server-side language such as PHP or ASP.Net to get the file list. You could then write this straight to the page, or use jQuery to make an AJAX request to get the data.

2 Comments

"jQuery is a client-side language" – JavaScript is the language, jQuery is just a JavaScript library :-)
"ASP.NET is a server-side language" - C# is the language, ASP.NET is just a C# library :-)
0

It actually is possible, IF the server returns an index page for the folder.

Just grab all anchor references on the page.

Here's an example: (modified from something similar found online a week or two ago)

var paintingarray = [];
var dir = "paintings";
var fileextension=".jpg";
$.ajax({
    //This will retrieve the contents of the folder if the folder is configured as 'browsable'
    url: dir,
    success: function (data) {
        //List all jpg files on the page
            $(data).find("a:contains(" + fileextension + ")").each(function () {
             var filename = this.href.replace(window.location.host,"").replace(window.location.pathname, "").replace("http://","");              

            paintingarray.push(dir + "/"+filename);

        });
    }
});

1 Comment

Yes this was an old post. However this would have worked at the time, and still works now.

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.