Determine if gravatar will return the default image using javascript

var gravatar1;
var gravatar2;
var email1 = $(email1).val();
email1 = $.trim(email1);
email1 = email1.toLowerCase();
email1 = md5(email1);
gravatar1 = 'http://www.gravatar.com/avatar/' + email1;
var email2 = $(email2).val();
email2 = $.trim(email2);
email2 = email2.toLowerCase();
email2 = md5(email2);
gravatar2 = 'http://www.gravatar.com/avatar/' + email2;

Is there a way to determine if gravatar1 returns the default image so that I can override gravatar1 to display the gravatar2 image. gravatar2 only overrides gravatar1 if the default image is returned. Thanks!

+4
source share
1 answer

If you add d = 404 to the url, gravatar will return the status 404 instead of the default image if the user does not have a set of images.

i.e. http://www.gravatar.com/avatar/098f6bcd4621d373cade4e832627b4f6?s=80&d=404 (user image is not available)

Will return 404.

While

http://www.gravatar.com/avatar/c9ef50b85bd345ea4e0d8da558816f3d?s=80&d=404 ( )

.

, , img.gravatar-img, src, d = 404, , (: HEAD) src,

404, , src , i.e

<img src="" data-defaultImg="">

- src , , , gravatar , , " " JS ajax .

$("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"));
    }
  });
});
+7

Source: https://habr.com/ru/post/1606710/


All Articles