I am developing an app that embeds users’ Facebook profile picture(http://graph.facebook.com/(fb id)/picture) inside an img tag. And it works…for the most part. However I will occasionally get 400 errors when I request the profile picture(see this)
Is there any way I can detect these 400 errors with Javascript/JQuery? Right now they are being displayed as a broken image….
This is pretty simple using the
onerrorevent.We can simply create a new image in JavaScript and attach two events to it –
onloadandonerror. Then setting the imagesrcwill make the browser download the image, firing theonloadevent if it is a true image, oronerrorif not.Demo: http://jsfiddle.net/amustill/vSJ4F/