faqts : Computers : Programming : Languages : JavaScript : Links

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

6 of 11 people (55%) answered Yes
Recently 3 of 8 people (38%) answered Yes

Entry

How can I locally generate a page containing links to files in a local directory?

Dec 4th, 2002 19:11
Ryan Buterbaugh, Rey Nuņez, Guillaume Bathedou,


I think I understand the question.  The protocol (it behaves like one,
but I'm not sure if it is strictly a protocol) file should do the 
job.  
You would write something like this (in javascript): 
file:///c:/file/path.ext.  I don't know what you would do with this 
though, because Javascript can't read normal files (i dont think/i 
hope).  It can only read files that are written in javascript (or 
probably vbscript, etc).  So <script 
src="file:///c:/somewhere/script.js"></script> would find a javascript 
on the users hard drive, but thats about it.  If the file isn't a 
script file, you get errors galore.
----------------
If you meant getting the href destination of all links on a page 
(which 
returns local file names when tested locally), you can use the links 
collection.
The collection retrieves a read-only array, in source order, 
containing 
all AREA and anchor (A) elements in a document with a value for the 
href attribute. 
Typical syntax is
   linksRef = document.links
The following example shows how the links collection is used to return 
the href values of all the links in the document where this code is in.
<script language="JavaScript">
<!--
function getLinks(){
var url = 'This page contains the following links:\n';
for (l=0; l<document.links.length; l++)
  url += '\n' + document.links[l].href;
return (url)
}
//-->
</script>
<a href="javascript:getLinks()">Get Links</a>