Forum Moderators: open

Message Too Old, No Replies

Check if image file exists?

...dynamically when using input type=file

         

Noisehag

8:36 pm on Jul 29, 2004 (gmt 0)

10+ Year Member



I've been on the hunt for a simple little function that will detect whether or not an image file exists when a user selects the file from their hard drive or types in the location. Just need it to alert that there is no such file if it truly does not exist. IE only.

Any takers? Bernard, you out there? :)

Bernard Marx

10:50 am on Jul 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes..and for quite a while now too.

I think the onerror event handler is what you want. Create an image object, associated with a local variable...


var img = new Image()
img.onload = function(){imageExists(true)}
img.onerror = function(){imageExists(false)}
img.src = input_src_string

function imageExists(exists)
{
if(exists)
//do this
else
// do that
}

Noisehag

6:22 pm on Jul 31, 2004 (gmt 0)

10+ Year Member



Thank you Bernard. I'll have a go at it in a few days and let you know. :)