0

I'm trying to create an HTML page that will change depending on what is in the current directory. There is an image box, and buttons that change the image. But because this page is identical throughout different pages, is there any way to add or remove these buttons depending on how many images are in the same directory?

The idea is that the site will then be expandable with minimal effort for changing each page, such that other users can do it. In the file below, it is the changePage button that needs to be removed.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head id="head">
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>
            Maths Study and Revision
        </title>
        <link href="./styles.css" rel="stylesheet" type="text/css">
        <script type="text/javascript">
        function changePage(page) {
            var img = document.getElementById("image");
            img.src="./pages/page_" + page + ".png";
            return false;
        }

    </script>
</head>
<body background="/img/image.jpg">
    <div id="container">
        <div id="header">
            <h1 style="border-style:solid;border-color:White;border-width:1px;">
                Maths Study and Revision
            </h1>
        </div>
        <div id="menuwrapper">
            <ul>
                <li>
                    <a href="#">Higher Tier</a>
                    <ul>
                        <li>
                            <a href="#">Calculator Paper</a>
                            <ul>
                                <li>
                                    <a href="/j13/hc/index.html">June 2013</a>
                                </li>
                                <li>
                                    <a href="/m13/hc/index.html">March 2013</a>
                                </li>
                                <li>
                                    <a href="/n12/hc/index.html">November 2012</a>
                                </li>
                                <li>
                                    <a href="/j12/hc/index.html">June 2012</a>
                                </li>
                            </ul>
                        </li>
                        <li>
                            <a href="#">Non-Calculator Paper</a>
                            <ul>
                                <li>
                                    <a href="/j13/hn/index.html">June 2013</a>
                                </li>
                                <li>
                                    <a href="/m13/hn/index.html">March 2013</a>
                                </li>
                                <li>
                                    <a href="/n12/hn/index.html">November 2012</a>
                                </li>
                                <li>
                                    <a href="/j12/hn/index.html">June 2012</a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li>
                    <a href="#">Foundation Tier</a>
                    <ul>
                        <li>
                            <a href="#">Calculator Paper</a>
                            <ul>
                                <li>
                                    <a href="/j13/fc/index.html">June 2013</a>
                                </li>
                                <li>
                                    <a href="/m13/fc/index.html">March 2013</a>
                                </li>
                                <li>
                                    <a href="/n12/fc/index.html">November 2012</a>
                                </li>
                                <li>
                                    <a href="/j12/fc/index.html">June 2012</a>
                                </li>
                            </ul>
                        </li>
                        <li>
                            <a href="#">Non-Calculator Paper</a>
                            <ul>
                                <li>
                                    <a href="/j13/fn/index.html">June 2013</a>
                                </li>
                                <li>
                                    <a href="/m13/fn/index.html">March 2013</a>
                                </li>
                                <li>
                                    <a href="/n12/fn/index.html">November 2012</a>
                                </li>
                                <li>
                                    <a href="/j12/fn/index.html">June 2012</a>
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
        <div id="intro">
            <p style="float:left;margin-left:10px;margin-bottom:10px;">
                <object type='text/plain' data='./title.html' height="50px" width="800px"></object>
            </p>
        </div>
        <div id="exam">
            <img src="./pages/page_1.png" id="image">
        </div>
        <div class="col1">
            <button onclick="changePage('1')">Page 1</button>                   
            <button onclick="changePage('2')">Page 2</button>
            <button onclick="changePage('3')">Page 3</button>
            <button onclick="changePage('4')">Page 4</button>
            <button onclick="changePage('5')">Page 5</button>
            <button onclick="changePage('6')">Page 6</button>
            <button onclick="changePage('7')">Page 7</button>
            <button onclick="changePage('8')">Page 8</button>
            <button onclick="changePage('9')">Page 9</button>
            <button onclick="changePage('10')">Page 10</button>
            <button onclick="changePage('11')">Page 11</button>         
            <button onclick="changePage('12')">Page 12</button>
            <button onclick="changePage('13')">Page 13</button>
            <button onclick="changePage('14')">Page 14</button>
            <button onclick="changePage('15')">Page 15</button>
            <button onclick="changePage('16')">Page 16</button>
            <button onclick="changePage('17')">Page 17</button>
            <button onclick="changePage('18')">Page 18</button>
            <button onclick="changePage('19')">Page 19</button>
            <button onclick="changePage('20')">Page 20</button>
            <button onclick="changePage('21')">Page 21</button>
            <button onclick="changePage('22')">Page 22</button>
            <button onclick="changePage('23')">Page 23</button>
            <button onclick="changePage('24')">Page 24</button>
        </div>
        <div id="footer">
        </div>
    </div>
</body>

Any help will be greatly appreciated!

OllieNye
  • 7
  • 1
  • 1
  • If you are doing this on the serverside you can use php, http://www.php.net/manual/en/function.scandir.php . If this is javascript, I guess you best bet will be to name the images like imageX.jpg, then let javascript download image0.jpg, ... imageN.jpg until you get a 404 response. – masterxilo Mar 24 '14 at 23:23
  • You COULD make an AJAX request or 20 to see if the images exist, but yes, it's probably better to do this server side. Use PHP, or Node, or Ruby, or Lua, or whatever you'd like to learn next. – MattDiamant Mar 24 '14 at 23:31

2 Answers2

0

You can't do this with JavaScript alone. You'll need to use server-side script in order to read the contents of the directory. If you're using PHP you can use the readdir function and then alter the HTML accordingly.

robbymarston
  • 344
  • 3
  • 16
0

This can be easily achieved using PHP and the scandir() method. However this requires a server to run your web page on (WAMP can be used to simulate a local server).

There is however an alternative to this by using HTML5. As previously posted here you can find tutorials describing the process on websites such as :

http://www.html5rocks.com/en/tutorials/file/filesystem/#toc-dir-reading

Community
  • 1
  • 1
Digzol
  • 323
  • 1
  • 12