jquery - 使用jquery加载头像

标签 jquery gravatar

只是想在博客上创建一个简单的评论表单。当用户在电子邮件框中写入此内容时,我想加载用户的头像(使用 jQuery)。

我怎样才能做到这一点?

最佳答案

头像网址如下所示:

http://www.gravatar.com/avatar/<md5hashofemail>

Here are the rest of the options for the URL.

因此,您所要做的就是包含一个名为 md5 的函数,该函数返回用户电子邮件的 md5 哈希值。网上有很多这样做的,但我相信https://github.com/blueimp/JavaScript-MD5/blob/master/README.md效果很好。之后,只需执行以下操作:

// get the email
var email = $('#email').val();
// -- maybe validate the email? 

// create a new image with the src pointing to the user's gravatar
var gravatar = $('<img>').attr({src: 'http://www.gravatar.com/avatar/' + md5(email)});
// append this new image to some div, or whatever
$('#my_whatever').append(gravatar);

// OR, simply modify the source of an image already on the page
$('#image').attr('src', 'http://www.gravatar.com/avatar/' + md5(email));

我认为这是显而易见的,但为了后代我将添加它:

如果用户电子邮件是私有(private)的,并且您在列表中显示此 ala-stackoverflow,那么您最好在服务器上对电子邮件进行编码,以便在查看源代码时用户电子邮件不会公开可见。

关于jquery - 使用jquery加载头像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/705344/

相关文章:

javascript - 如何将 "n"定义为任何自然基数变量

javascript - 拖放不适用于溢出自动

javascript - 平滑向下滚动

javascript - 用头像表达

php - 如何获取URL以发布作者的用户BuddyPress Avatar在Wordpress发布循环中

javascript - 使 gif 出现在单击按钮上

javascript - ActionView::MissingTemplate::缺少模板 - 部分 Java 脚本

WordPress:本地主机上的自定义默认头像?

gravatar - 如何在 Stack Overflow 上生成随机默认值 "gravatars"?

android - 使用 picasso 获取用户的Facebook个人资料图片