Yahoo Answers is shutting down on 4 May 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

JavaScript Images (Advanced JS)?

I need a way to view the contents of a picture folder on a local drive using just JavaScript and HTML. Basically i have some pics in a folder and i want to display them on the page, without having to link to each one specifically. It's for a project I'm doing on the PSP, any help will be credited, thank you.

Update:

You can use JavaScript to access files on the PSP, remote or not, i know this because I've hosted files which read .js files on the PSP. The point is i was opening a specific address and didn't have to worry about file names, now i just want to display all pictures in a folder. Which i can only archive through specific address'. I want something that will just find all pictures and display them, which is possible on a PC just doesn't work on a PSP with the script i used.

Update 2:

Thanks for being so specific with the PHP code, however I need JS, i can't use PHP on the PSP.

2 Answers

Relevance
  • 1 decade ago
    Favourite answer

    To my knowledge this cannot be done with JavaScript. You'll need to use a server-side script, like PHP:

    <?php

    Header("content-type: application/x-javascript");

    function returnimages($dirname=".")

    {

      $pattern="(\.jpg$) | (\.png$) | (\.jpeg$) | (\.gif$)";

      $files = array();

      $curimage=0;

      if ($handle = opendir($dirname))

      {

        while (false !== ($file = readdir($handle)))

        {

          if (eregi($pattern, $file))

          {

            echo 'galleryarray['.$curimage.'] = "'.$file .'";';

            $curimage++;

          }

        }

        closedir($handle);

      }

      return($files);

    }

    echo 'var galleryarray=new Array();';

    returnimages()

    ?>

    This will grab the file names of the images in a directory and output an array, like this:

    var galleryarray = new Array();

    galleryarray[0] = 'myimage.gif';

    ...

    galleryarray[n] = 'anotherimage.jpg';

    ---

    Of course you link to it from your HTML like this:

    <script src="getimages.php"></script>

    You can write another javscript to loop through the array and display each image on the page when the page has finished loading... should be simple enough.

    > Like I said, what you are asking is beyond the capability of JavaScript. The only way to do what you're asking is by manually typing out the array of images, and changing the JavaScript source every time new images are added or removed.

    However if your PSP has a connection to the internet and the PHP and images are hosted on a web server, as long as the web server is capable of interpreting PHP - your PSP won't even know it's a PHP file because the mime type is being set to JavaScript on the server, thus the file will render as if it were a .js file.

    Source(s): Professional Web Developer
  • Anonymous
    1 decade ago

    Browser based, client side javascript cannot access anything (aside from cookies) on the local filesystem.

Still have questions? Get answers by asking now.