Connecting Tech Pros Worldwide Help | Site Map

Javascript read web directory

  #1  
Old August 17th, 2006, 01:55 PM
gencode
Guest
 
Posts: n/a
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,

  #2  
Old August 17th, 2006, 02:55 PM
Tom Cole
Guest
 
Posts: n/a

re: Javascript read web directory


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.

  #3  
Old August 18th, 2006, 08:55 AM
p.lepin@ctncorp.com
Guest
 
Posts: n/a

re: Javascript read web directory



gencode wrote:
Quote:
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

  #4  
Old August 21st, 2006, 10:45 PM
gencode
Guest
 
Posts: n/a

re: Javascript read web directory


Thanks Roy

p.lepin@ctncorp.com wrote:
Quote:
gencode wrote:
Quote:
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
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript Read Content Directory Hannes answers 5 June 27th, 2008 08:12 PM
detect pdf properties using javascript, possible? mscir answers 4 July 23rd, 2005 12:23 PM
Changing session cookie via javascript? M Wells answers 3 July 20th, 2005 03:17 PM