View Full Version : Can javascript search for and load images automatically
shashgo
09-21-2002, 01:30 PM
hi all,
I want to make a web page that displays a whole bunch of pictures that i have on my desktop. The count of pictures keeps increasing as I add more pictures. So I cannot use an array with a definite bound since there is no definite bound.
Is it possible to write javascript code that can load all the pictures, without me having to give it the names of the pictures?
Ex: one picture might be sally.jpg, anoother may be john.jpg, a third might be henry.jpg. Typing each of these names into individual <img> tags would be very tedious, so i was hoping there is a better way.
If anyone knows let me know
BigTony
09-22-2002, 05:25 PM
I would say it's possible (but I'm not that good in JS so i'm not going to say that it's possible in that.)
However you would need a server with CGI, PHP, ASP or some other server side script installed. I've seen I done in PERL so you could probably find the code online.
jippie
09-23-2002, 04:59 AM
Hi,
it would have to be on serverside, but it is possible.
But you can do it a bit easier clientside.
Make a multiple string array, then all you have to do is add the names to a list instead..
Jippie
shashgo
09-23-2002, 07:34 PM
dbindel,
got any tips for me on this?
jippie
09-24-2002, 12:47 AM
Hi,
Clientside
<script language="JavaScript">
ImageList = new Array("images/1.jpg",
"images/2.jpg",
"images/3.jpg",
"images/4.jpg",
"images/5.jpg" );
for (var i = 0; i < ImageList.length; i++)
{
document.write("<img src=\""+ImageList[i]+"\" width=180 height=75>");
}
</script>
This is for serverside!!
http://www.faqts.com/knowledge_base/view.phtml/aid/1463/fid/189
Most people don't use serverside javascript..
Jippie
jippie
09-24-2002, 05:50 AM
Oh, sorry for my confusion.
shashgo
09-24-2002, 09:53 AM
To Dbindel and Jippie,
Dbindel, looks like youve exactly understood what I mean. There has to be a way using server side Javascript, I think. Worst come to worst, I can always list each file and load the pictures that way, but since the files are not sequentially numbered like 1.jpg, 2.jpg,... it does become tedious.
I know a little ASP, but probably not that much to know xactly what to write. Can you give me a little script? And would any server that hosts my webpage be able to support ASP or is that something I would need to check with them?
I dont know Php but if I need to I can learn it. So is there a good resourse on the net?
Jippie,
The code you wrote works, and that is what I had earlier when I had a few image files. But now with over a 100 files, I am loooking for another way.
shashgo
09-24-2002, 11:48 AM
All,
I just had another thought on the image loading thing I want to do.
I can use jippie's code for the image loading with a modification. While the loop goes through each number and loads 1.jpg, 2.jpg,3.jpg,4.jpg,... , if I have code that says for every nonexistant image, load the next one, then it may do what I want. What do you guys think? Can I use thee onerror event for doing this?
shashgo
09-24-2002, 01:54 PM
I was wondering: Is there a way to check whether an image exists or not, by comparing it to null or something?
Example:
for (i=0;i<5;i++)
{
image=new Image();
image.src=(i+1)+".jpg";
if (image==null)
document.write("Image "+(i+1)+".jpg not found");
else
document.write("<img src='image.src'><br>");
}
So if the image is not found, an error message is displayed otherwise the image is displayed. Is something like this possible?
jippie
09-25-2002, 04:43 AM
Hmm on error,
there is an on error event, but no idea if this is only IE blah blah blah
<IMG ONERROR="alert(this.src + ' ERROR')" SRC="not.jpg" NAME="imageName">
If the image not.jpg doesn't exist the alert box will open.
You can change the alert function to what ever.
BUT after all that I now don't know what you are trying to do with this???
Jippie
jippie
09-25-2002, 05:19 AM
dbindel or shashgo,
I can't test this in anyway, I have no servers to work from.
But this is a serverside object that can access folders!!
<%
var fso;
var files;
fso = Server.CreateObject ("Scripting.FileSystemObject");
files = fso.Getfolder( "path where the files are" );
for (var i = 0; i < files.length; i++)
{
document.write("<img src=\""+files[i]+"\" width=180 height=75>");
}
%>
Jippie
jippie
09-25-2002, 07:16 AM
Hi sorry one of those thing I got into.
This will list all the files in a directory but only server side.
Just fun but can be done :-)
<% @Language = "JavaScript" %>
<html>
<body>
<%
~~~~~~~~ CreateObject doesn't work!!!
var fso = new ActiveXObject("Scripting.FileSystemObject");
var folder = fso.Getfolder( "C:\\Inetpub\\wwwroot\\images" );
var en = new Enumerator(folder.Files);
Response.Write("<BR>");
Response.Write(folder);
Response.Write("<BR>");
while(!en.atEnd())
{
Response.Write(en.item().name); <<<< this is the file name :-)
Response.Write("<BR>");
en.moveNext()
}
%>
</body>
</html>
shashgo
09-25-2002, 08:28 AM
Jippie,
Thanks for helping me.
I was looking at the scrpt you gav me, and I wonder what it is. Its not javascript is it, cause what is the % for. I thought that was only used with ASP.
Let me know.
jippie
09-25-2002, 12:52 PM
Hi,
that's one of the strange things that I'm not sure about.
People call it "ASP" but I always thought it was serverside javascript.
and I thought there was serverside VBscript (lots od dim's :-)
ASP I always thought just ment that the file contained serverside script. be it Javascript or VBscript or now maybe PHP(?????)
PLEASE correct me if I'm wrong.....
Jippie
vBulletin® v3.7.2, Copyright ©2000-2008, Jelsoft Enterprises Ltd.