gencode wrote:
I need to make a javascript read a web directory from a
remote site (ie "http://remotesite.com/images")
(The remote die does not have an index.htm and does have
directory listing enabled)
I have seen many samples but they all use frames or
iframes, all I want is a method that you can say
<body
onload="readremotedir('http://remotesite.com/images');">
Here's an ugly hack for you. Note that it was only tested
with Apache/2.0.53 under MSIE 6.0 and Firefox 1.5. It
almost certainly won't work with other servers (and
probably even with different versions/configurations of
Apache). If you really want a robust function, you'll just
have to roll up your sleeves and make it work.
function get_apache_dirlist ( url )
{
var http ;
if ( window . XMLHttpRequest )
{
http = new XMLHttpRequest ( ) ;
}
else
{
http = new ActiveXObject ( 'microsoft.XMLHTTP' ) ;
}
http . open ( 'GET' , url , true ) ;
http . onreadystatechange =
function ( )
{
if ( http . readyState == 4 )
{
parse_apache_dirlist ( http . responseText ) ;
}
return ( true ) ;
} ;
http . send ( null ) ;
return ( true ) ;
}
function parse_apache_dirlist ( html )
{
var doc ;
pseudo_xml = new String
( '<' + '?xml version="1.0"?>' + html ) ;
pseudo_xml = pseudo_xml . replace
( /<!DOCTYPE .*?>/g , '' ) ;
pseudo_xml = pseudo_xml . replace
( /<(\w)r>/g , '<$1r />' ) ;
pseudo_xml = pseudo_xml . replace
( /<img (.*?)>/g , '<img $1 \/>' ) ;
if ( window . DOMParser )
{
doc = new DOMParser ( ) ;
doc = doc . parseFromString
( pseudo_xml , 'text/xml' ) ;
}
else
{
doc = new ActiveXObject ( 'microsoft.XMLDOM' ) ;
doc . loadXML ( pseudo_xml ) ;
}
var anchors = doc . getElementsByTagName ( 'a' ) ;
var hrefs = new Array ( ) ;
for ( var i = 0 ; i < anchors . length ; i ++ )
{
var href = anchors [ i ] . getAttribute ( 'href' ) ;
if ( ! href . match ( /^[\?\/]/ ) )
{
hrefs . push ( href ) ;
}
}
return ( hrefs ) ;
}
--
roy axenov