Displaying files in a folder

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ca1icoJack
    New Member
    • Mar 2009
    • 5

    Displaying files in a folder

    I'm new to Javascript and want to write a script which will loop through the files in a folder and display them in a webpage. I've written the following which alternately displays two images in a folder, but I want to be able to add new files to that folder and have them added to this loop. I'm assuming I just need some code to populate the list of files in the folder, but don't know how to do that, nor how to make var have a variable max depending on the number of files. This will all be displayed locally, so there's no kind of web server.

    Code:
    <html>
    
        <head>
    
            <title>CMM Reference Part Check Compliance</Title>
    
            <script type="text/javascript">
                <!--
                var image1=new Image()
                image1.src="Slide1.gif"
                var image2=new Image()
                image2.src="Slide2.gif"
                //-->
            </script>
    
        </head>
    
        <body>
    
            <img src="Slide1.gif" name="slide" width="95%" height="95%" />
    
            <script>
                <!--
                    //variable that will increment through the images
                    var step=1
    
                    function slideit() {
                        //if browser does not support the image object, exit.
                        if (!document.images)
                            return
                        document.images.slide.src=eval("image"+step+".src")
                        if (step<2)
                            step++
                        else
                            step=1
                        //call function "slideit()" every 10 seconds
                        setTimeout("slideit()",1000)
                    }
    
                    slideit()
                //-->
            </script>
                
        </body>
    
    </html>
    How might I do this?

    Thanks very much for your help
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I don't think that javascript can read a directory, but if you name the files consistently you can request each of it with a loop.

    Comment

    • Ca1icoJack
      New Member
      • Mar 2009
      • 5

      #3
      Thats part of the issue, the names won't be consistent since they'll be related to all sorts of different things. What would I use instead then? PHP was mentioned somewhere but I know absolutely nothing about that.

      Comment

      • Canabeez
        New Member
        • Jul 2009
        • 126

        #4
        Here,

        put this code in images.php, and place the file in your images directory.
        [code=php]
        <?php
        header("Content-Type: text/javascript");
        $AllowedExtensi ons = array(".gif", ".jpg", ".png", "jpeg");
        echo "var images = new Array();";
        $d = dir("./");
        while (false !== ($entry = $d->read()))
        {
        if($entry != '.' && $entry != '..' && is_file($entry) && in_array(substr ($entry,-4), $AllowedExtensi ons))
        {
        echo "images.push('{ $entry}');";
        }
        }
        $d->close();
        ?>[/code]
        Then just call this file within <SCRIPT>
        [code=html]
        <script type="text/javascript" src="images/images.php"></script>[/code]

        Comment

        • Ca1icoJack
          New Member
          • Mar 2009
          • 5

          #5
          Thanks very much, but how do I use that in the HTML? Where do I set the time delay between updating etc? (I'm new to all of this)

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by Ca1icoJack
            Thanks very much, but how do I use that in the HTML?
            just call the script (as Canabeez posted)

            Originally posted by Ca1icoJack
            Where do I set the time delay between updating etc? (I'm new to all of this)
            that's a question how you set the cache instructions. probably
            Code:
            // in images.php
            header('Cache-Control: must-revalidate');
            could suffice (more Cache-Control options). Cache instructions may also be set by the server.

            Comment

            Working...