If you add a d=404 to the url gravatar will return a 404 status instead of a default image if the user does not have an image set.
i.e. http://www.gravatar.com/avatar/098f6bcd4621d373cade4e832627b4f6?s=80&d=404 (user image not available)
Will return a 404.
While
http://www.gravatar.com/avatar/c9ef50b85bd345ea4e0d8da558816f3d?s=80&d=404 (user image available)
Will return the image.
So you can do the below, which basically iterates through all img.gravatar-img elements, gets the src, sets the d=404 mentioned above, then sends a request(type:HEAD) for the src,
if the page returns a 404, the error block will run allowing you to replace the src of the image with one available, i personally have the default image on the image i.e
<img src="" data-defaultImg="">
or the other way around - you can set the src to the default image replace the error block with a success block, thus if the gravatar image exists replace the default image, this prevents showing a "not found" image on the page if JS is disabled or if the ajax call takes time.
$("img.gravatar-img").each(function(i,el) {
imgUrl = $(el).attr("src") + "&d=404";
// Image Does Not Exist
$.ajax({
url:imgUrl,
type:"HEAD",
crossDomain:true,
error:function(){
$(el).attr("src", $(el).attr("data-defaultimg"));
}
});
});