views:

482

answers:

5

i have to display auto-generated images from a folder in html.

how can i check if file exist in javascript/jquery so i can do a for-loop and display them all. they will be numbered 1.jpg, 2.jpg and so on.

+3  A: 

You can probably fire a request and check the HTTP status code (404 for not existing ;))

Thomas Wanner
damn, i forgot i could do an ajaxcall to a php that checks for me and return status:)
weng
You don't even need to do that, just do a head request for the resource and check the header that comes back.
Rich Bradshaw
+1  A: 

Check this solution to the same question: http://stackoverflow.com/questions/197228/how-to-check-if-a-file-exists-in-javascript

cwaidner
+1  A: 

If You don't like the ajax and checking http status method, there is a HTML-DOM way to do that.

if You put a new img node and add onload - it will fire only when/if image loads.

in jQuery:

var there=$('get where You want to put it');

$('<img />').attr('src',image_link).bind('load',function(){
 do whatever You need to do if image loads
 }).appendTo(there);

and it starts the function with "do whatever" after image loads.

cheers;)

naugtur
+1  A: 

I think you can use the onerror event of the img tag.
It will be triggered if the img is not loaded properly.

Within this event you could remove this(these) img and stop the loop.

Mic
A: 

Are you talking about client or server side javascript?

:->

herzmeister der welten
Please post comments as comments, not as answers :)
BalusC