prashant said:
I am looking to determine the number of image files ina
folder so that I could create a dynamic table accordingly. For this i
need a method to identify the number of files in a folder. Is there
any way i could estimate this so that i could determine the size of an
array to create my tables
There are possibilities if the server allows to list the files in the
directory and if the default index-file is absent.
I made a demo for you:
http://www.dotinternet.be/temp/test.htm
The directory is:
http://www.dotinternet.be/temp/images/
The trick is to find some filter to make sure which text can be
considered safe enough to correspond to one image. In this case, I've
chosen for 'ALT=""'.
I don't say that this demo is the preferred way. You will have a
shorter and more solid solution when doing the job in your favourite
server-side scripting language.
--------------------------------------------------------------------
Start Code
--------------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Number of images</title>
<script type="text/javascript">
var xmlhttp;
function load(url)
{
xmlhttp=null;
// code for Mozilla, etc.
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
// code for IE
else if (window.ActiveXObject)
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}
function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
{
// if "OK"
if (xmlhttp.status==200)
{
var nr = xmlhttp.responseText.split('ALT="[IMG]"');
alert( (nr.length-1) + ' images');
}
else
{
alert("Problem retrieving resource");
}
}
}
</script>
</head>
<body onload="load('images/?' + new Date().getTime());">
<p> </p>
</body>
</html>
--------------------------------------------------------------------
End Code
--------------------------------------------------------------------
In another post you wrote that script and directory can reside on
different domains. The demo above will not work then because
XMLHttpRequest is subject to the Same Origin Policy. You can use
www.ajax-cross-domain.com to overcome this problem.
Hope this helps,