使用jquery加载gravatar

Tim*_*uge 24 jquery gravatar

只是想在博客上创建一个简单的评论表单.我想在他/她在电子邮箱中写入时加载用户的gravatar(使用jQuery).

我怎样才能做到这一点?

Pao*_*ino 52

gravatar url看起来像这样:

http://www.gravatar.com/avatar/<md5hashofemail>
Run Code Online (Sandbox Code Playgroud)

以下是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));
Run Code Online (Sandbox Code Playgroud)

我认为这很明显,但我会为了后人的缘故加上它:

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

  • 我会保存你的谷歌搜索:http://www.webtoolkit.info/javascript-md5.html (4认同)