Connecting Tech Pros Worldwide Help | Site Map

Javascript read web directory

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 17th, 2006, 12:55 PM
gencode
Guest
 
Posts: n/a
Default Javascript read web directory

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, 01:55 PM
Tom Cole
Guest
 
Posts: n/a
Default 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, 07:55 AM
p.lepin@ctncorp.com
Guest
 
Posts: n/a
Default 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, 09:45 PM
gencode
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.