Javascript read web directory

G

gencode

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');">

and the method would get the directory listings and populate an array
var Pic = new Array()

Sorry in advance for something that may be easy, I know little about
Javascript

Are there any samples that dont use frames/iframes

Thnanks, Ed,
 
T

Tom Cole

What server do you use? Typically the server is going to send back some
HTML formatted response that is not consistent with other servers.

But the general gist is to create a XMLHttpRequest to send the request
to the specified URL asynchronously. Then in your callback method read
the response. This is where you'll need to know something about what
the server sends back. Parse that response and use it to build your
HTML output.

Are you familiar with XMLHttpRequests? If so this should be pretty
easy, if not, then you get to learn something new.

I know it's a pretty generic response, but HTH anyway. Without more
details about what the server is sending back to you it's hard to be
much more help.
 
P

p.lepin

gencode said:
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 ) ;
}
 
G

gencode

Thanks Roy

gencode said:
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 ) ;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top