Entry
Can JavaScript generate a listing of links to all files in a directory?
Using the server-side javascript example to generate a directory listing, the page returns the full
Mar 3rd, 2000 00:56
Martin Honnen, Gary Pooley,
For client side JavaScript the answer is no, server side JavaScript has
access to the web server's file system and can easily generate the
links for a directory listing.
The only thing client side JavaScript can do is load the directory url
in a (hidden) frame or layer and if the web server generates a
directory listing traverse the document.links array. This is of course
of limited use as the web server already needs to serve a directory
listing. The following is an example which uses a hidden frame in a
frameset to generate a listing of the directory url passed in with the
query string e.g. save the document as
dirListing.html
and load for example
dirListing.html?/directoryName
<HTML>
<HEAD>
<SCRIPT>
function collectLinks () {
var links =
dirListing.document && dirListing.document.links ?
dirListing.document.links : new Array();
return links;
}
function writeLinks () {
var links = collectLinks();
var d = main.document;
d.open();
for (var l = 0; l < links.length; l++)
d.write('<A HREF="' + links[l].href + '">' +
(links[l].text ? links[l].text : links[l].href) +
'<\/A><BR>');
d.close();
}
var url = location.search ? location.search.substring
(1) : 'about:blank';
var html = '';
html +=
'<FRAMESET ONLOAD="writeLinks()" ROWS="100%, *" FRAMEBORDER="0">';
html +=
'<FRAME NAME="main" SRC="about:blank" SCROLLING="auto" NORESIZE>';
html +=
'<FRAME NAME="dirListing" SRC="' + url + '" NORESIZE SCROLLING="no">';
html += '<\/FRAMESET>';
</SCRIPT>
</HEAD>
<SCRIPT>
document.write(html);
</SCRIPT>
</HTML>
All that is limited of course to directories on the same server the
page is loaded from.