0

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 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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

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;)

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.